diff --git a/chapters/intro.xml b/chapters/intro.xml index ae2ffaeb1de3..3673ec28e100 100644 --- a/chapters/intro.xml +++ b/chapters/intro.xml @@ -1,203 +1,193 @@ - + + - Introduction - What is PHP and what can it do? + What is PHP and what can it do? + Introduction +
- What is PHP? - - PHP (recursive acronym for PHP: Hypertext - Preprocessor) is a widely-used open source general-purpose - scripting language that is especially suited for web - development and can be embedded into HTML. - - - Nice, but what does that mean? An example: - - + + What is PHP? + + + + PHP (a recursive acronym for + PHP: Hypertext Preprocessor) + is a widely used open-source general-purpose scripting language + especially suited for web development, and it can be embedded directly into HTML. + + + + What does that mean in practice? Consider the following example: + + - An introductory example - -An introductory example + - - Example - - + + Example + + - + - + -]]> - + ]]> - - - Instead of lots of commands to output HTML (as seen in C or Perl), - PHP pages contain HTML with embedded code that does - something (in this case, output Hi, I'm a PHP script!). - The PHP code is enclosed in special start and end processing - instructions <?php and ?> - that allow jumping in and out of PHP mode. - - - What distinguishes PHP from something like client-side JavaScript - is that the code is executed on the server, generating HTML which - is then sent to the client. The client would receive - the results of running that script, but would not know - what the underlying code was. A web server can even be configured - to process all HTML files with PHP, and then there's no - way that users can tell that PHP is being used. - - - The best part about using PHP is that it is extremely simple - for a newcomer, but offers many advanced features for - a professional programmer. Don't be afraid to read the long - list of PHP's features. With PHP, almost anyone can get up and running and be - writing simple scripts in no time at all. - - - Although PHP's development is focused on server-side scripting, - much more can be done with it. Read on, and see more in the - What can PHP do? section, - or go right to the introductory - tutorial to jump straight to learning about web programming. - + + + Instead of using many commands to output HTML (as in languages such as C or Perl), + PHP pages contain regular HTML with embedded code that performs + actions (in this case, outputting + Hi, I'm a PHP script!). + The PHP code is enclosed within special processing instructions + <?php and ?> that allow switching into and out of + PHP mode. + + + + Unlike client-side JavaScript, PHP code is executed on the server, + generating HTML that is then sent to the client. The client sees + only the final output and not the underlying PHP code. + A web server can even be configured to process all HTML files through PHP, + making it impossible for users to know that PHP is being used. + + + + One advantage of PHP is that it is extremely simple for beginners + while still offering advanced features for experienced developers. + With PHP, almost anyone can begin writing useful scripts quickly. + + + + Although PHP’s primary focus is server-side scripting, + it is capable of much more. You can read more in the + What can PHP do? section, + or jump straight into the introductory tutorial + to start learning web programming. + +
+
- What can PHP do? - - Anything. PHP is mainly focused on server-side scripting, - so it can do anything any other CGI program can do, such - as collect form data, generate dynamic page content, or - send and receive cookies. But PHP can do much more. - - - There are two main areas where PHP scripts are used. + + What can PHP do? + + + + PHP is capable of performing anything that other CGI programs can do, + such as collecting form data, generating dynamic page content, + or sending and receiving cookies. However, PHP goes far beyond these capabilities. + + + + PHP scripts are mainly used in two primary areas: + + - - - Server-side scripting. This is the most widely used - and main target field for PHP. Three things are needed - to make this work: the PHP parser (CGI or server - module), a web server, and a web browser. All these can - run on a local machine in order to just experiment - with PHP programming. See the - installation instructions - section for more information. - - - - - Command line scripting. A PHP script can be run - without any server or browser, only the - PHP parser is needed to use it this way. - This type of usage is ideal for scripts regularly - executed using cron (on Unix or macOS) or Task Scheduler (on - Windows). These scripts can also be used for simple text - processing tasks. See the section about - Command line usage of PHP - for more information. - - + + + Server-side scripting. + This is PHP’s most common use case. To run server-side scripts, + you need three components: the PHP parser (as a CGI binary or a server module), + a web server, and a web browser. All of these can run locally for testing and development. + For details, refer to the installation instructions. + + + + + + Command-line scripting. + PHP scripts can also run on the command line, without any web server or browser. + This is ideal for scripts executed using tools such as cron + on Unix/macOS or Task Scheduler on Windows. + PHP is also suitable for text-processing tasks. + See Command line usage of PHP for more information. + + - - - PHP can be used on all major operating systems, including - Linux, many Unix variants (including HP-UX, Solaris and OpenBSD), - Microsoft Windows, macOS, RISC OS, and probably others. - PHP also has support for most of the web servers today. This - includes Apache, IIS, and many others. And this includes any - web server that can utilize the FastCGI PHP binary, like lighttpd - and nginx. PHP works as either a module, or as a CGI processor. - - - So with PHP, developers have the freedom of choosing an operating - system and a web server. Furthermore, they also have the choice - of using procedural programming or object-oriented - programming (OOP), or a mixture of them both. - - - PHP is not limited to outputting HTML. PHP's abilities include - outputting rich file types, such as images or PDF files, encrypting data, - and sending emails. It can also output easily any text, such as JSON - or XML. PHP can autogenerate these files, and save them in the - file system, instead of printing it out, forming a server-side cache for - dynamic content. - - - One of the strongest and most significant features in PHP is its - support for a wide range of databases. - Writing a database-enabled web page is incredibly simple using one of - the database specific extensions (e.g., for mysql), - or using an abstraction layer like PDO, or connect - to any database supporting the Open Database Connection standard via the - ODBC extension. Other databases may utilize - cURL or sockets, - like CouchDB. - - - PHP also has support for talking to other services using protocols - such as LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM (on Windows) and - countless others. It can also open raw network sockets and - interact using any other protocol. PHP has support for the WDDX - complex data exchange between virtually all Web programming - languages. Talking about interconnection, PHP has support for - instantiation of Java objects and using them transparently - as PHP objects. - - - PHP has useful text processing features, - which includes the Perl compatible regular expressions (PCRE), - and many extensions and tools to parse and access XML documents. - PHP standardizes all of the XML extensions on the solid base of libxml2, - and extends the feature set adding SimpleXML, - XMLReader and XMLWriter support. - - - And many other interesting extensions exist, which are categorized both - alphabetically and by category. - And there are additional PECL extensions that may or may not be documented - within the PHP manual itself, like XDebug. - - - This page is not enough to list all - the features and benefits PHP can offer. Read on in - the sections about installing - PHP, and see the function - reference part for explanation of the extensions - mentioned here. - + + + PHP runs on all major operating systems, including Linux, numerous Unix variants + (such as HP-UX, Solaris, and OpenBSD), Microsoft Windows, macOS, and others. + It also supports a wide range of web servers including Apache, IIS, lighttpd, and nginx. + PHP can operate either as a module or as a CGI/FastCGI processor. + + + + PHP gives developers the freedom to choose their operating system, + web server, and programming paradigm. You may write procedural code, + object-oriented programs, or a combination of both. + + + + PHP is not limited to generating HTML. It can create images, PDF files, + and other rich content types; encrypt data; and send emails. + It can output text formats such as XML or JSON and can generate files for + server-side caching. + + + + One of PHP’s strongest features is its support for a + wide range of databases. + Using extensions such as MySQLi, + abstraction layers such as PDO, + or the ODBC extension, + making database-enabled web pages becomes straightforward. + Other databases can be accessed using cURL + or sockets, such as CouchDB. + + + + PHP supports communication with services using protocols such as LDAP, + IMAP, SNMP, NNTP, POP3, HTTP, COM (on Windows), and more. + It can also open raw network sockets for custom protocols. + PHP supports WDDX for data exchange between different programming languages. + It can instantiate Java objects and interact with them through a PHP interface. + + + + PHP includes powerful text processing features, + including PCRE-compatible regular expressions + (PCRE) + and multiple extensions for working with XML. XML functionality is built on + libxml2 and includes + SimpleXML, + XMLReader, + and XMLWriter. + + + + Many other useful extensions exist, listed both + alphabetically + and by category. + Additional extensions are available through PECL + (PECL extensions), + including tools such as Xdebug. + + + + This chapter cannot list every feature PHP offers. Read the sections on + installing PHP and consult the + function reference for more details + about the features and extensions mentioned here. + +
-
- - +
diff --git a/chapters/tutorial.xml b/chapters/tutorial.xml index bafbdb5b4ddf..c914d8c6ca24 100644 --- a/chapters/tutorial.xml +++ b/chapters/tutorial.xml @@ -1,67 +1,81 @@ - - A simple tutorial + + + + A simple tutorial + - Here we would like to show the very basics of PHP in a short, simple - tutorial. This text only deals with dynamic web page creation with - PHP, though PHP is not only capable of creating web pages. See - the section titled What can PHP - do for more information. + Here we would like to show the very basics of PHP in a short, simple + tutorial. This text only deals with dynamic web page creation with + PHP, though PHP is not only capable of creating web pages. See + the section titled What can PHP + do for more information. + - PHP-enabled web pages are treated just like regular HTML pages and - you can create and edit them the same way you normally create - regular HTML pages. + PHP-enabled web pages are treated just like regular HTML pages and + you can create and edit them the same way you normally create + regular HTML pages.
- Your first PHP-enabled page - - This tutorial assumes PHP is already installed. - Installation instructions can be found on the - download page. - - - Create a file named hello.php - with the following content: - - + + Your first PHP-enabled page + + + + This tutorial assumes PHP is already installed. + Installation instructions can be found on the + download page. + + + + Create a file named hello.php + with the following content: + + - Our first PHP script: <filename>hello.php</filename> - - + Our first PHP script: <filename>hello.php</filename> + + + -]]> - - - Using your terminal, navigate to the directory containing this file and - start a development server with the following command: - - - + + + Using your terminal, navigate to the directory containing this file and + start a development server with the following command: + + + - - - Use your browser to access the file with your web server's URL, ending - with the /hello.php file reference. - According to the previous command executed, the URL will be - http://localhost:8000/hello.php. - If everything is configured correctly, this file will be parsed by PHP - and you will see the "Hello World!" output displayed in your browser. - - - PHP can be embedded within a normal HTML web page. That means inside your HTML document - you can write the PHP statements, as demonstrated in the following example: - - - + + + Use your browser to access the file with your web server's URL, ending + with the /hello.php file reference. + According to the previous command executed, the URL will be + http://localhost:8000/hello.php. + If everything is configured correctly, this file will be parsed by PHP + and you will see the "Hello World!" output displayed in your browser. + + + + PHP can be embedded within a normal HTML web page. That means inside + your HTML document you can write PHP statements, as demonstrated in the + following example: + + + @@ -71,13 +85,13 @@ php -S localhost:8000 Hello World

'; ?> -]]> -
- - This will result in the following output: - - -
+ + + This will result in the following output: + + + @@ -87,165 +101,181 @@ php -S localhost:8000

Hello World

-]]> -
+]]>
-
- - This program is extremely simple and you really did not need to use - PHP to create a page like this. All it does is display: - Hello World using the PHP echo - statement. Note that the file does not need to be executable - or special in any way. The server finds out that this file needs to be interpreted - by PHP because you used the ".php" extension, which the server is configured - to pass on to PHP. Think of this as a normal HTML file which happens to have - a set of special tags available to you that do a lot of interesting things. - - - - The point of the example is to show the special PHP tag format. - In this example we used <?php to indicate the - start of a PHP tag. Then we put the PHP statement and left PHP mode by - adding the closing tag, ?>. You may jump in - and out of PHP mode in an HTML file like this anywhere you want. For more - details, read the manual section on the - basic PHP syntax. - - - - A Note on Line Feeds + - Line feeds have little meaning in HTML, however it is still a good idea - to make your HTML look nice and clean by putting line feeds in. A - linefeed that follows immediately after a closing - ?> will be removed by PHP. This can be extremely - useful when you are putting in many blocks of PHP or include files - containing PHP that aren't supposed to output anything. At the same time - it can be a bit confusing. You can put a space after the closing - ?> to force a space and a line feed to be output, - or you can put an explicit line feed in the last echo/print from within - your PHP block. + This program is extremely simple and you really did not need to use + PHP to create a page like this. All it does is display + Hello World using the PHP echo + statement. Note that the file does not need to be executable + or special in any way. The server finds out that this file needs to be interpreted + by PHP because you used the ".php" extension, which the server is configured + to pass on to PHP. Think of this as a normal HTML file which happens to have + a set of special tags available to you that do a lot of interesting things. - - - A Note on Text Editors - There are many text editors and Integrated Development Environments (IDEs) - that you can use to create, edit and manage PHP files. A partial list of - these tools is maintained at PHP Editors - List. If you wish to recommend an editor, please visit the above - page and ask the page maintainer to add the editor to the list. Having - an editor with syntax highlighting can be helpful. + The point of the example is to show the special PHP tag format. + In this example we used <?php to indicate the + start of a PHP tag. Then we put the PHP statement and left PHP mode by + adding the closing tag, ?>. You may jump in + and out of PHP mode in an HTML file like this anywhere you want. For more + details, read the manual section on the + basic PHP syntax. - - - A Note on Word Processors + + + A Note on Line Feeds + + + Line feeds have little meaning in HTML, however it is still a good idea + to make your HTML look nice and clean by putting line feeds in. A + line feed that follows immediately after a closing + ?> will be removed by PHP. This can be extremely + useful when you are putting in many blocks of PHP or include files + containing PHP that aren't supposed to output anything. At the same time + it can be a bit confusing. You can put a space after the closing + ?> to force a space and a line feed to be output, + or you can put an explicit line feed in the last echo/print from within + your PHP block. + + + + + + A Note on Text Editors + + + There are many text editors and Integrated Development Environments (IDEs) + that you can use to create, edit and manage PHP files. A partial list of + these tools is maintained at + PHP Editors List. + If you wish to recommend an editor, please visit the above + page and ask the page maintainer to add the editor to the list. + Having an editor with syntax highlighting can be helpful. + + + + + + A Note on Word Processors + + + Word processors such as StarOffice Writer, Microsoft Word and Abiword are + not optimal for editing PHP files. If you wish to use one for this + test script, you must ensure that you save the file as plain + text or PHP will not be able to read and execute the script. + + + - Word processors such as StarOffice Writer, Microsoft Word and Abiword are - not optimal for editing PHP files. If you wish to use one for this - test script, you must ensure that you save the file as plain - text or PHP will not be able to read and execute the script. + Now that you have successfully created a working PHP script, it is + time to create the most famous PHP script. Make a call to the + phpinfo function and you will see a lot of useful + information about your system and setup such as available + predefined variables, + loaded PHP modules, and configuration + settings. Take some time and review this important information. - - - - Now that you have successfully created a working PHP script, it is - time to create the most famous PHP script! Make a call to the - phpinfo function and you will see a lot of useful - information about your system and setup such as available - predefined variables, - loaded PHP modules, and configuration - settings. Take some time and review this important information. - - + - Get system information from PHP - - + Get system information from PHP + + -]]> - +]]> - +
- Something Useful - - Let us do something more useful now. We are going to check - what sort of browser the visitor is using. - For that, we check the user agent string the browser - sends as part of the HTTP request. This information is stored in a variable. Variables always start - with a dollar-sign in PHP. The variable we are interested in right now - is $_SERVER['HTTP_USER_AGENT']. - - + + Something Useful + + + + Let us do something more useful now. We are going to check + what sort of browser the visitor is using. + For that, we check the user agent string the browser + sends as part of the HTTP request. This information is stored in a + variable. Variables always start + with a dollar sign in PHP. The variable we are interested in right now + is $_SERVER['HTTP_USER_AGENT']. + + + + + $_SERVER is a + special reserved PHP variable that contains all web server information. + It is known as a superglobal. See the related manual page on + superglobals + for more information. + + + - $_SERVER is a - special reserved PHP variable that contains all web server information. - It is known as a superglobal. See the related manual page on - superglobals - for more information. + To display this variable, you can simply do: - - - To display this variable, you can simply do: - - + - Printing a variable (Array element) - - + Printing a variable (Array element) + + + -]]> - +]]> + + + A sample output of this script may be: + + + + + - A sample output of this script may be: + There are many types of + variables available in PHP. In the above example we printed an element + from an Array variable. + Arrays can be very useful. - - - - - - - There are many types of - variables available in PHP. In the above example we printed an element - from an Array variable. - Arrays can be very useful. - - - $_SERVER is just one variable that PHP automatically - makes available to you. A list can be seen in the - Reserved Variables section - of the manual or you can get a complete list of them by looking at - the output of the phpinfo function used in the - example in the previous section. - - - You can put multiple PHP statements inside a PHP tag and create - little blocks of code that do more than just a single echo. - For example, if you want to check for Firefox you - can do this: - - + + + $_SERVER is just one variable that PHP automatically + makes available to you. A list can be seen in the + Reserved Variables section + of the manual or you can get a complete list of them by looking at + the output of the phpinfo function used in the + example in the previous section. + + + + You can put multiple PHP statements inside a PHP tag and create + little blocks of code that do more than just a single echo. + For example, if you want to check for Firefox you + can do this: + + - Example using <link linkend="language.control-structures">control - structures</link> and <link linkend="language.functions">functions</link> - - + Example using <link linkend="language.control-structures">control + structures</link> and <link linkend="language.functions">functions</link> + + + -]]> - - - A sample output of this script may be: - - - + + + A sample output of this script may be: + + + - +]]> - - - Here we introduce a couple of new concepts. We have an - if statement. - If you are familiar with the basic syntax used by the C - language, this should look logical to you. Otherwise, you - should probably pick up an introductory PHP book and read the first - couple of chapters, or read the Language - Reference part of the manual. - - - The second concept we introduced was the str_contains - function call. str_contains is a function built into - PHP which determines if a given string contains another string. In this case we are - looking for 'Firefox' (so-called needle) inside - $_SERVER['HTTP_USER_AGENT'] (so-called haystack). If - the needle is found inside the haystack, the function returns true. Otherwise, it - returns &false;. If it returns &true;, the if expression evaluates to &true; - and the code within its {braces} is executed. Otherwise, the code is not - run. Feel free to create similar examples, - with if, - else, and other - functions such as strtoupper and - strlen. Each related manual page contains examples - too. If you are unsure how to use functions, you will want to read both - the manual page on how to read a - function definition and the section about - PHP functions. - - - We can take this a step further and show how you can jump in and out - of PHP mode even in the middle of a PHP block: - - + + + Here we introduce a couple of new concepts. We have an + if statement. + If you are familiar with the basic syntax used by the C + language, this should look logical to you. Otherwise, you + should probably pick up an introductory PHP book and read the first + couple of chapters, or read the Language + Reference part of the manual. + + + + The second concept we introduced was the str_contains + function call. str_contains is a function built into + PHP which determines if a given string contains another string. In this case we are + looking for 'Firefox' (the needle) inside + $_SERVER['HTTP_USER_AGENT'] (the haystack). If + the needle is found inside the haystack, the function returns + true. Otherwise, it returns false. + When the function returns true, the + if expression evaluates to + true and the code within its braces is executed; otherwise, that block is + skipped. Feel free to create similar examples + with if, + else, and other + functions such as strtoupper and + strlen. Each related manual page contains examples + too. If you are unsure how to use functions, you will want to read both + the manual page on how to read a + function definition and the section about + PHP functions. + + + + We can take this a step further and show how you can jump in and out + of PHP mode even in the middle of a PHP block: + + - Mixing both HTML and PHP modes - - + Mixing both HTML and PHP modes + + + @@ -316,45 +350,51 @@ if (str_contains($_SERVER['HTTP_USER_AGENT'], 'Firefox')) { -]]> - - - A sample output of this script may be: - - - + + + A sample output of this script may be: + + + str_contains() returned true

You are using Firefox

-]]> -
+]]>
-
- - Instead of using a PHP echo statement to output something, we jumped out - of PHP mode and just sent straight HTML. The important and powerful point - to note here is that the logical flow of the script remains intact. Only - one of the HTML blocks will end up getting sent to the viewer depending on - the result of str_contains. In other words, it depends on - whether the string Firefox was found or not. - + + + Instead of using a PHP echo statement to output + something, we jumped out of PHP mode and just sent straight HTML. The + important and powerful point to note here is that the logical flow of the + script remains intact. Only one of the HTML blocks will end up getting + sent to the viewer depending on the result of + str_contains, that is, whether the string + Firefox was found or not. + +
- Dealing with Forms - - One of the most powerful features of PHP is the way it handles HTML - forms. The basic concept that is important to understand is that any - form element will automatically be available to your PHP - scripts. Please read the manual section on - Variables from external - sources for more information and examples on using forms - with PHP. Here is an example HTML form: - - + + Dealing with Forms + + + + One of the most powerful features of PHP is the way it handles HTML + forms. The basic concept that is important to understand is that any + form element will automatically be available to your PHP + scripts. Please read the manual section on + Variables from external + sources for more information and examples on using forms + with PHP. Here is an example HTML form: + + - A simple HTML form - - + A simple HTML form + + + @@ -364,73 +404,80 @@ if (str_contains($_SERVER['HTTP_USER_AGENT'], 'Firefox')) { -]]> - +]]> - - - There is nothing special about this form. It is a straight HTML form - with no special tags of any kind. When the user fills in this form - and hits the submit button, the action.php page - is called. In this file you would write something like this: - - + + + There is nothing special about this form. It is a straight HTML form + with no special tags of any kind. When the user fills in this form + and hits the submit button, the action.php page + is called. In this file you would write something like this: + + - Printing data from our form - - + Printing data from our form + + + . You are years old. -]]> - - - A sample output of this script may be: - - - + + + A sample output of this script may be: + + + - +]]> - - - Apart from the htmlspecialchars and - (int) parts, it should be obvious what this does. - htmlspecialchars makes sure any characters that are - special in html are properly encoded so people can't inject HTML tags - or Javascript into your page. For the age field, since we know it is a - number, we can just convert - it to an int which will automatically get rid of any - stray characters. You can also have PHP do this for you automatically by - using the filter extension. - The $_POST['name'] and $_POST['age'] - variables are automatically set for you by PHP. Earlier we - used the $_SERVER superglobal; above we just - introduced the $_POST - superglobal which contains all POST data. Notice how the - method of our form is POST. If we used the - method GET then our form information would live in - the $_GET superglobal instead. - You may also use the $_REQUEST - superglobal, if you do not care about the source of your request data. It - contains the merged information of GET, POST and COOKIE data. - + + + Apart from the htmlspecialchars and + (int) parts, it should be obvious what this does. + htmlspecialchars makes sure any characters that are + special in HTML are properly encoded so people cannot inject HTML tags + or JavaScript into your page. For the age field, since we know it is a + number, we can just convert + it to an int which will automatically get rid of any + stray characters. You can also have PHP do this for you automatically by + using the filter extension. + The $_POST['name'] and $_POST['age'] + variables are automatically set for you by PHP. Earlier we + used the $_SERVER superglobal; above we just + introduced the $_POST + superglobal which contains all POST data. Notice how the + method of our form is POST. If we used the + method GET then our form information would live in + the $_GET superglobal instead. + You may also use the $_REQUEST + superglobal if you do not care about the source of your request data. It + contains the merged information of GET, POST and COOKIE data. + +
- What's next? - - With your new knowledge you should be able to understand most of - the manual and also the various example scripts available in the - example archives. - - - To view various slide presentations that show more of what PHP can do, - see the PHP Conference Material Site: - &url.php.talks; - + + What's next? + + + + With your new knowledge you should be able to understand most of + the manual and also the various example scripts available in the + example archives. + + + + To view various slide presentations that show more of what PHP can do, + see the PHP Conference Material Site: + http://talks.php.net/ + +
-
+ +