首页 PHP

PHP

举报
开通vip

PHPChapitre 1 Introduction to PHP This Chapter will be able to : know what the dynamic web is know how PHP was created know how to install PHP Presentation Introduction to Web At the begin, static sites emmerged from the web, their maintenanced and dev...

PHP
Chapitre 1 Introduction to PHP This Chapter will be able to : know what the dynamic web is know how PHP was created know how to install PHP Presentation Introduction to Web At the begin, static sites emmerged from the web, their maintenanced and development were limited. Today static sites are used essentially by window sites which need little updating. Introduction to dynamic Web Contrary to static stites, dynamic web allows one to generate and manage the contents of site pages with ease. At present more and more sites tend to become dynamic websites. The arrival of blogs and other MySpace is largely responsible for this. PHP has contributed largely to the development of dynamic web. History of PHP PHP was created in 1994 by Rasmus Lerdorf . Originally it was a PERL software library, which he used to count the number of visit to his CV. He added several functionnalities such as the capacity to communicate with databases. He published his code for the first time in 1995. In 1997, two students, Andi Gutmans and Zeev Suraski , redeveloped PHP/FI. They created PHP 3, rebaptised PHP: Hypertext Preprocessor . In 1999 creation of the Zend Engine , heart of PHP 4. Current version : 5.2.8 - December 2008. The support of version 4 of PHP was totally abandonned at the version 4.4.9 on 08 August 2008. Functioning of PHP PHP is an intepreted language which generated HTML. This signifies that the client sends a request to the server (to display the page), the said server receives the request, transmits it to the PHP interpreter which is charged with generating a page HTML, it will then send back to the server which transmits it to the client. In practice, all this routes is invisible to the client, and this traitement is done almost instantaneously or in a few seconds. Development Tools Server Web Currently many web servers can host sites developed in PHP. The most used and most well-known is none other than Apache, but other web servers exist such as IIS, Lighttpd, Nginx... For all the developments of local PHP websites their exist full solutions such as EasyPHP and Wamp for Windows, Lamp for Linux and Mamp for Mac OS. Database Adding PDO in version 5.1 of PHP has allowed for the simplification of the interaction with a database. Today it has become easy for users to work with databases such as Oracle, MySQL, SQLite, PostgreSQL, DB2, SQL Server,... IDE (éditors) IDE or Integrated Development Environment, in the case of PHP, will permit you to develop more efficiently. Many exist for all platforms. Netbean (Multi plateform) jEdit (Multi plateform) Eclipse (Multi plateform) Notepad++ (Windows) Smultron (Mac) Textmate (Mac) Frameworks A Framework is a group of libraries. Its aim is to provide a series of functions whose objective is to furnish a working space for the creation of all, or part, of a software system. The term "Framework" is not specific to PHP, many Frameworks exist for other languages (C#, Java, JavaScript, Ruby, ...). Below is a non exhaustive list of Framework for PHP: Zend Framework Symfony Cake PHP Jelix Code Igniter PHP on Trax Chapitre 2 PHP Language This Chapter will be able to : know the syntax use php possibilities Basic syntax Tags and comments PHP source code must be placed inside PHP tags. 4 kinds of those tags are possible: This tag is deprecated. There is a problem with XML declaration. shortcut of: Few editors do not support this syntax <% ? %> Some other languages use this tag. <%="Hello World" %> shortcut of <% echo "Hello World" %> Look at this script: Example 1. Exemple view plaincopy to clipboardprint? Say hello Say hello It allows you to see many basic rules of PHP. First of all, each PHP instruction must be ended by a semi-colon. It is one of most common errors. But the last instruction does not need necessary one because the ending PHP tag contains an implicit one: Example 2. Exemple view plaincopy to clipboardprint? It is recommended to comment your codes. You can use some one-line comments or multi-line comments: Example 3. Exemple view plaincopy to clipboardprint? At last, as we already mentioned it, only the source code inside PHP tags are interpreted and empty lines inside these tags are ignored by the PHP engine. Variables PHP variables begin with a $, and the character just after must be a letter or an underscore. Then letters, underscore and numbers can be used. For example, $supinfo , $Supinfo , $_Supinfo are valid variables. Nevertheless, $123titi and $456 are not valid ones. PHP variables are also case sensitive. It means that $supinfo is also different from $Supinfo and $SUPINFO . To define a value to a variable, you have to use the operator: = Example 4. Exemple view plaincopy to clipboardprint? It is possible to define simultaneously to many variables a same value: Example 5. Exemple view plaincopy to clipboardprint? It is possible to use in the same time an arithmetic operator and the = operator: Example 6. Exemple view plaincopy to clipboardprint? It is possible to increase or diminish the value of a variable with increment and decrement operators: Example 7. Exemple view plaincopy to clipboardprint? To concatenate strings the operator is the point ( . ). Example 8. Exemple view plaincopy to clipboardprint? '; print $resultat; // Show PHP is happiness ;) ?> Furthermore, you can use the point to concatenate with the operator = in the same time: Example 9. Exemple view plaincopy to clipboardprint? It is possible to destroy a variable using unset ($var). It is also possible to destroy an array , and you can choose if you destroy only an element or the array himself. Example 10. Exemple view plaincopy to clipboardprint? Types In many languages, it is mandatory to indicate the variable type before using it. With PHP it is not the case, because the type is determined in function of the context use. This does not prevent PHP from having different types of variables. PHP supports 8 types of variables: 4 scalar types: Boolean : That?s the simplest one. A Boolean can have two values: TRUE or FALSE. Integer : An integer is a part of real numbers. It is possible to specify integers by these different ways: decimal (base 10), hexadecimal (base 16), octal (base 8) and may be preceded by the sign minus (-). Float : Floating point numbers can be specified. Their size depends on the platform. A maximum of ~1.8e308 with a precision of 14 decimals is a classic configuration: it is the 64 bits IEEE format. String : A string is a character chain. PHP does not have a native Unicode support (this support is planned for PHP 6). A string may be specified by three different ways: Simple quotes : The easiest way to specify a string is to use simple quotes. To write a simple quote inside the string, you can use the backslash character (\). Then if you want to write a backslash inside the string you have to double it. Double quotes : If the string is specified with double quotes, PHP will understand some special sequences like carriage return, tabulations ? Furthermore, if some variables are in the string, they will be replaced by their values. HereDoc syntax : Another way to specify a string is the syntax called "Here doc" <<<, followed by an arbitrary identifier, and then followed by the string. This sequence is ending by the initial identifier, placed first on a new line. Warning, the line with the ending PHP tag must not contain any other character. 2 Types Composed: Array : A PHP array is container in which you can precise values in function of keys. Object 2 Special Types: Resource : A resource is a special type that represents a reference to an external resource. Null : Null means that there is no value. There are few special functions that concerns variable types: string gettype ( var ) : return the type of the variable as a string. Be careful not doing some tests on it (using if or switch) because the value could change from a PHP version to another. Example 11. Exemple view plaincopy to clipboardprint? The following code will show string integer boolean . bool is_type ( var ) : return true if the variable is defined with the correct type. Here the functions : is_array() is_bool() is_float() is_int() is_null() is_object() is_real() is_resource() is_numeric() is_scalar() is_string() For example: Example 12. Exemple view plaincopy to clipboardprint? $int = 5; is_int($int); // true is_string($int); // false $int = 5; is_int($int); // true is_string($int); // false var_dump ( var ) : return structured information of a variable, with its type and value. Example 13. Exemple view plaincopy to clipboardprint? The following code shows: int (15). Constants A constant is a name representing a simple value. This value can not be modified. Constant names have same rules than variables. By convention, these names are always uppercase. To declare a constant, we use define () function: Example 14. Exemple view plaincopy to clipboardprint? Predefined constants These constants are defined directly in PHP and can be called in your script without declaration. Here some predefined constants: PHP_VERSION DEFAULT_INCLUDE_PATH PHP_EXTENSION_DIR E_STRICT These constants are often PHP options. Example 15. Exemple view plaincopy to clipboardprint? Some predefined constants change in function of the place they are called. These are magic constants and there are 5. Table 1. Constantes magiques Name Description __LINE__ The current line number of the file __FILE__ The full path and filename of the file __FUNCTION__ The function name __CLASS__ The class name __METHOD__ The class method name If our folder is c:\www\monsite and our file name is index.php, so the __FILE__ constant show the full path of index.php. Example 16. Exemple view plaincopy to clipboardprint? Operators There are many kinds of operators. However we are going to study only main ones : Calculation operators: Table 2. Calculation operators Operator Action Example Result + Sum $a + $b Sum of $a and $b. - Subtraction $a - $b Subtraction of $a et $b. * Multiplication $a * $b Multiplication of $a et $b. / Division $a / $b Quotient de $a et $b. % Modulo $a % $b The rest of $a divided by $b. ++ Pre incrémentation ++$a Increment $a by 1, then return $a. ++ Post-incrémentation $a++ Return $a then increment by 1. -- Pre décrémentation --$a Decrement $a by 1, then return $a. -- Post-décrémentation $a-- Return $a, then decrement $a by 1. Comparison operators: Table 3. Comparison operators Operator Action Example Result == Equal $a == $b True is $a is equal to $b === Identical $a === $b True is $a and $b are equal and from the same type. != The difference $a != $b True if $a and $b don?t have same values. <> The difference $a <> $b True if $a and $b don?t have same values. < Lower than $a < $b True is $a is lower than $b <= Lower than or equal $a <= $b True if $a is lower than or equal to $b > Greater than $a > $b True if $a is greater than $b >= Greater than or equal $a >= $b True if $a is greater than or equal to $b Logical operators: Table 4. Logical operators Operator Example Result AND $a and $b TRUE if $a AND $b are TRUE OR $a or $b TRUE if $a OR $b are TRUE XOR $a xor $b TRUE if $a OR $b are TRUE but not both NOT ! $a TRUE if $a is FALSE AND $a && $b TRUE if $a AND $b are TRUE OR $a || $b TRUE if $a OR $b are TRUE Control structures if ... elseif ... else If the command is checked, instructions are executed. Example 1. Exemple view plaincopy to clipboardprint? if instruction may end by endif . Example 2. Exemple view plaincopy to clipboardprint? else complete the if one. If the command is checked in if , then instructions are executed. Else, other instructions are executed. Example 3. Exemple view plaincopy to clipboardprint? elseif structure allows you to choose between many instruction blocks. Example 4. Exemple view plaincopy to clipboardprint? It is possible to imbricate many if instructions to test many conditions. Example 5. Exemple view plaincopy to clipboardprint? It is also possible to write many logical operators inside the condition. Example 6. Exemple view plaincopy to clipboardprint? Ternary operator ?: allows you to make a shortcut to a simple if ? else block. The syntax is similarly to: condition ? if condition is true : if condition is false. Then the following code: Example 7. Exemple view plaincopy to clipboardprint? Can be: Example 8. Exemple view plaincopy to clipboardprint? switch switch is the ideal to choose between many instruction blocks. If all tests fail, there is a default block which allows executing code: Example 9. Exemple view plaincopy to clipboardprint? Warning : PHP continues to execute instructions until the end of the switch or until the first break it find. If there is no break at the end of a case , all next case would be executed until the end of the switch or until a break . while. do ... while while execute a set of instructions until the condition is true . Example 10. Exemple view plaincopy to clipboardprint? If the condition is false at the beginning the code will never be executed. To execute the code and then test the condition ( while is the opposite), there is do ? while . Example 11. Exemple view plaincopy to clipboardprint? for Look at the following syntax: Example 12. Exemple view plaincopy to clipboardprint? The first expression (expr1) is always executed. At the beginning of each iteration, the expression expr2 is evaluated. If this evaluation is true , the loop continues and instructions executed. If the evaluation is false , the loop is stopped. At the end of each iteration, the expression expr3 is executed. Expressions can be empty. Warning : if the expression expr2 is empty, it means that the loop will never end! foreach foreach allows you to make a loop on an array as the same way than for, but you don?t have to manage an index yourself. foreach works on a copy of the array. It means that a modification of the array inside the foreach won?t be effective on the array outside the loop. In the following example, instructions will be executed as many times as the number of elements in the array $my_array . At each instructions call, $element will take the value of the current element of the array. Example 13. Exemple view plaincopy to clipboardprint?
本文档为【PHP】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_147093
暂无简介~
格式:doc
大小:301KB
软件:Word
页数:50
分类:互联网
上传时间:2010-03-26
浏览量:51