1. PHP-HYPERTEXT PREPROCESSOR PHP: Hypertext Preprocessor is a widely used, general-purpose scripting language that was originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document. As a general-purpose programming language, PHP code is processed by an interpreter application in command-line mode performing desired operating system operations and producing program output on its standard output channel. It may also function as a graphical application. PHP is available as a processor for most modern web servers and as standalone interpreter on most operating systems and computing platforms. PHP was originally created by Rasmus Lerdorf in 1995 and has been in continuous development ever since. The main implementation of PHP is now produced by The PHP Group and serves as the de facto standard for PHP as there is no formal specification. PHP is free software released under the PHP License.
2. Usage PHP is a general-purpose scripting language that is especially suited to server-side web development where PHP generally runs on a web server. Any PHP code in a requested file is executed by the PHP runtime, usually to create dynamic web page content. It can also be used for command-line scripting and client-side GUI applications. PHP can be deployed on most web servers, many operating systems and platforms, and can be used with many relational database management systems. It is available free of charge, and the PHP Group provides the complete source code for users to build, customize and extend for their own use. PHP primarily acts as a filter, taking input from a file or stream containing text and/or PHP instructions and outputs another stream of data; most commonly the output will be HTML. Since PHP 4, the PHP parser compiles input to produce bytecode for processing by the Zend Engine, giving improved performance over its interpreter predecessor. Originally designed to create dynamic web pages, PHP now focuses mainly on server-side scripting, and it is similar to other server-side scripting languages that provide dynamic content from a web server to a client, such as Microsoft's Active Server Pages, Sun Microsystems' JavaServer Pages, and mod perl. PHP has also attracted the development of many frameworks that provide building blocks and a design structure to promote rapid application development (RAD). Some of these include CakePHP, Symfony, CodeIgniter, and Zend Framework, offering features similar to other web application frameworks.
3. How To Install PHP On Linux This tutorial explains the installation of PHP 5, bundled with Apache and MySQL server on a Linux machine. The tutorial was written primarily for SuSE 9.2, 9.3, 10.0 & 10.1, but most of the steps ought to be valid for all Linux-like operating systems. We will set up PHP as a shared module, being loaded into Apache2 dynamically during the server startup. These instructions are known to work for PHP versions: 5.0.4 through 5.2.1. Prerequisites At this point Apache web server must be installed. If you want MySQL support in PHP, MySQL server also must have been installed prior to the next steps. Download Source Get the source from http://www.php.net/downloads.php . At the time of writing this tutorial the best available version was 5.2.1 ( php-5.2.1.tar.gz ).
4. Unpack, Configure, Compile Go to the directory whith the downloaded file and enter: # tar -xzf php-5.2.1.tar.gz # cd php-5.2.1 # ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql The configuration options ought to be self-explaining; --prefix specifies the location where PHP is to be installed, --with-apxs2 with correct path pointing to bin/apxs in the Apache installation directory is mandatory for the installator to work. Since PHP 5, you need to explicitly bundle PHP with MySQL by --with-mysql directive (make sure you specified path to where MySQL is installed on your system). There are many other options which turn on additional features. For all available configuration options and their default values type ./configure --help.
5. TIP : If you are performing an upgrade, you may want to copy config.nice from the old PHP installation directory (if available) to where you unpacked the new PHP tarball file. Run ./config.nice instead of ./configure. This way all the previous configure options will be applied to the new installation effortlessly. Once you entered ./configure with all the options you need, compile and install the software: # make # make install
6. Edit Httpd.conf All necessary changes to httpd.conf (Apache configuration file) should have already been made automatically during the installation, so usually you need not do anything. Nevertheless, check that following lines were added to the httpd.conf file: LoadModule php5_module modules/libphp5.so AddType application/x-httpd-php .php If not, add them manually. Create Php.ini File Importanly, you have to create a php.ini configuration file. Choose one of the pre-made files (preferably php.ini-recommended) residing inside the php-5.2.1/ directory (it's the folder to which the downloaded archive was extracted). Copy the file to the lib/ directory in the PHP installation directory. # cp php-5.2.1/php.ini-recommended /usr/local/php/lib/php.ini If you need to, edit the php.ini file: # vi /usr/local/php/lib/php.ini However, the default settings should work for everyone in most cases.
7. Restart Apache Server After everything is set up, restart Apache: # /usr/local/bin/apachectl restart Further Reading PHP Manual Do you have an idea how to make these instructions better? Did you run into any problems or have any tips? You are welcome to share your experience in the comment s.
9. PHP's initialization file, generally called php.ini, is responsible for ; configuring many of the aspects of PHP's behavior. PHP attempts to find and load this configuration from a number of locations. The following is a summary of its search order: 1. SAPI module specific location. 2. The PHPRC environment variable. (As of PHP 5.2.0) 3. A number of predefined registry keys on Windows (As of PHP 5.2.0) 4. Current working directory (except CLI) 5. The web server's directory (for SAPI modules), or directory of PHP (otherwise in Windows) 6. The directory from the --with-config-file-path compile time option, or the Windows directory (C:indows or C:innt) See the PHP docs for more specific information. http://php.net/configuration.file The syntax of the file is extremely simple. Whitespace and Lines beginning with a semicolon are silently ignored (as you probably guessed). Section headers (e.g. [Foo]) are also silently ignored, even though they might mean something in the future.
10. Directives following the section heading [PATH=/www/mysite] only apply to PHP files in the /www/mysite directory. Directives following the section heading [HOST=www.example.com] only apply to PHP files served from www.example.com. Directives set in these special sections cannot be overridden by user-defined INI files or at runtime. Currently, [PATH=] and [HOST=] sections only work under CGI/FastCGI. http://php.net/ini.sections Directives are specified using the following syntax: directive = value Directive names are *case sensitive* - foo=bar is different from FOO=bar. Directives are variables used to configure PHP or PHP extensions. There is no name validation. If PHP can't find an expected directive because it is not set or is mistyped, a default value will be used. The value can be a string, a number, a PHP constant (e.g. E_ALL or M_PI), one of the INI constants (On, Off, True, False, Yes, No and None) or an expression (e.g. E_ALL & ~E_NOTICE), a quoted string ("bar"), or a reference to a previously set variable or directive (e.g. ${foo})
11. About this file PHP comes packaged with two INI files. One that is recommended to be used ; in production environments and one that is recommended to be used in ; development environments. ; php.ini-production contains settings which hold security, performance and ; best practices at its core. But please be aware, these settings may break ; compatibility with older or less security conscience applications. We ; recommending using the production ini in production and testing environments. ; php.ini-development is very similar to its production variant, except it's ; much more verbose when it comes to errors. We recommending using the ; development version only in development environments as errors shown to ; application users can inadvertently leak otherwise secure information.
12. Quick Reference The following are all the settings which are different in either the production ; or development versions of the INIs with respect to PHP's default behavior. ; Please see the actual settings later in the document for more details as to why ; we recommend these changes in PHP's behavior. ; allow_call_time_pass_reference ; Default Value: On ; Development Value: Off ; Production Value: Off ; display_errors ; Default Value: On ; Development Value: On ; Production Value: Off ; display_startup_errors ; Default Value: Off ; Development Value: On ; Production Value: Off ; error_reporting ; Default Value: E_ALL & ~E_NOTICE ; Development Value: E_ALL | E_STRICT ; Production Value: E_ALL & ~E_DEPRECATED
13. l og_errors ; Default Value: Off ; Development Value: On ; Production Value: On ; magic_quotes_gpc ; Default Value: On ; Development Value: Off ; Production Value: Off ; max_input_time ; Default Value: -1 (Unlimited) ; Development Value: 60 (60 seconds) ; Production Value: 60 (60 seconds) ; output_buffering ; Default Value: Off ; Development Value: 4096 ; Production Value: 4096 ; register_argc_argv ; Default Value: On ; Development Value: Off ; Production Value: Off ; register_long_arrays ; Default Value: On ; Development Value: Off ; Production Value: Off
14. php.ini Options Name for user-defined php.ini (.htaccess) files. Default is ".user.ini" ;user_ini.filename = ".user.ini" ; To disable this feature set this option to empty value ;user_ini.filename = ; TTL for user-defined php.ini files (time-to-live) in seconds. Default is 300 seconds (5 minutes) ;user_ini.cache_ttl = 300 Language Options This directive determines whether or not PHP will recognize code between ; <? and ?> tags as PHP source which should be processed as such. It's been ; recommended for several years that you not use the short tag "short cut" and ; instead to use the full <?php and ?> tag combination. With the wide spread use ; of XML and use of these tags by other languages, the server can become easily ; confused and end up parsing the wrong code in the wrong context. But because ; this short cut has been a feature for such a long time, it's currently still ; supported for backwards compatibility, but we recommend you don't use them.
16. PHP VARIABLES Variable Types Variables play an important role in PHP, as they are places for holding values. In PHP, there is no need to declare variables. Variable can hold eight different data types: bloolean, integer, float, string, array, object, resource, or NULL. PHP is a weakly typed language. This means that variable type varies depending on what is stored in the variable at the time. For example, if we have a variable $a, when $a = 0, $a is an integer type variable. If later we set $a = "New", then $a becomes a string type variable. Variable Name A variable name always starts with a $, followed by a letter or an underscore. The rest of the variable name can be a letter, a number or an underscore. For example, $dog is a valid variable name, while @dog is not (@dog does not start with a $). Variables in PHP are case-sensitive. For example, $Employee and $employee are two different variables.
17. PHP OPERATORS Assignment Operators The basic assignment operator in PHP is "=". This means that the operand to the left of "=" gets set to the value to the right of "=". Arithmetic Operators Operator Example Result + 4 + 2 6 - 4 - 2 2 * 4 * 2 8 / 4 / 2 2 % 4 % 20 ++ x = 4; x++; x = 5 -- x = 4; x--; x = 3 Combined Operators You can combine an arithmetic operator with the assignment operator to form a combined operator. Combined operators are shown below: Operator Example Meaning += y += x y = y + x -= y -= x y = y - x *= y *= x y = y * x /= y /= x y = y / x %= y %= x y = y % x
18. Comparison Operators Operator Meaning == is equal to != is not equal to > is greater than >= is greater than or equal to < is less than <= is less than or equal to Logical Operators Operator Meaning || or && and and and or or xor xor ! not
19. PHP IF ELSE IF..ELSE is used in PHP to provide conditional judgements. The basic syntax is as follows: IF (conditional statement) { [code if condition is true] } ELSE { [code if condition is false] } Let's see an example. Assuming we have the following piece of code: $sample = 10; IF ($sample > 5) { print "Number is greater than 5"; } ELSE { print "Number is less than 5"; } The output of the above code is: Number is greater than 5
20. PHP FUNCTIONS Similar to other programming languages, PHP provides a way for programmers to define functions, which can then be called elsewhere in the program. The syntax for a function is: function "function_name" (arg1, arg2...) { [code to execute] return [final_result]; } where [final_result] is typically the variable holding the final value to be returned from the function. Let's take a look at an example: function double_this_number($input_number) { return $input_number*2; } Elsewhere in the PHP code, we have $x = 10; $y = double_this_number($x); print $y; The output will be 20
21. PHP ARRAYS An array is a way of holding multiple closely-related values, such as the test scores of all students in a class. An array is made up of a key and a value, and the key points to the value. There are two types of arrays: Indexed array and Associative array. Their difference is in the way the key is specified. Let's look at both of them: Indexed Array In an indexed array, the keys are numeric and starts with 0, and the values can be any data type. The following shows two ways of assigning values to an indexed array: $friends = array("Sophie","Stella","Alice"); This is equivalent to the following: $friends[0] = "Sophie"; $friends[1] = "Stella"; $friends[2] = "Alice";
22. PHP FORMS One of the main features in PHP is the ability to take user input and generate subsequent pages based on the input. In this page, we will introduce the mechanism by which data is passed in PHP. Let's consider the following two files: query.php <form action=result.php type=post> <input type=text name=employee> <input type=submit value=Submit> </form> result.php <?php $employee_name = $_POST["employee"]; print $employee_name; ?>
23. PHP COOKIES Create cookies Cookies are set using the setcookie() function. The syntax is as follows: Setcookie (name, value, expire, path, domain, secure) name = name of the cookie. value = value of the cookie. expire = time when this cookie will expire. Unix time is used here. path = the path on the server on which the cookie is available. domain = the domain that the cookie is available. secure = TRUE means the cookie should be trasmitted over a secure connection (https), FALSE otherwise. FALSE is the default. All arguments except name are optional. Unix time is the number of seconds that have elapsed since January 1, 1970. You must make sure that the setcookie() function is called before any HTML output is printed. Let's take a look at a couple of examples: <?php setcookie('cookie1','lisa'); ?>
24. PHP MYSQL The PHP code needed is as follows (assuming the MySQL Server sits in localhost and has a userid = 'cat' and a password of 'dog', the database name is 'myinfo') : $link = @mysql_pconnect("localhost","cat","dog") or exit(); mysql_select_db("myinfo") or exit(); print "<p>Employee Information"; print "<p><table border=1><tr><td>Employee Name</td><td>Salary Amount</td></tr>"; $result = mysql_query("select name, salary from Employee"); while ($row=mysql_fetch_row($result)) { print "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td></tr>"; } print "</table>"; Output is Employee Information Employee Name Salary Amount Lisa 40000 Alice 45000 Janine 60000
25. PHP SYNTAX DO ... WHILE DO { [code to execute] } WHILE (conditional statement) ELSEIF IF (conditional statement 1) { [code if condition statement 1 is true] } ELSEIF (conditional statement 2) { [code if condition statement 2 is true] } ELSE { [code if neither statement is true] } FOR Loop FOR (expression 1, expression 2, expression 3) { [code to execute] }
26. FOREACH Loop FOREACH ($array_variable as $value) { [code to execute] } or FOREACH ($array_variable as $key => $value) { [code to execute] } IF ELSE IF (conditional statement) { [code if condition is true] } ELSE { [code if condition is false] }
27. Include INCLUDE ("external_file_name"); SWITCH ($variable) { CASE 'value 1': [code to execute when $variable = 'value 1'] break; CASE 'value 2': [code to execute when $variable = 'value 2'] break; CASE 'value 3': ... DEFAULT: [code to execute when none of the CASE values matches $variable'] } WHILE Loop WHILE (expression) { [code to execute] }