PHP Introduction
PHP is a powerful tool for making dynamic and interactive Web pages.
PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.
In our PHP tutorial you will learn about PHP, and how to execute scripts on your server.
PHP is a server-side scripting language.
What You Should Already Know ?
Before you continue you should have a basic understanding of the following:
* HTML/XHTML
* JavaScript
If you want to study these subjects first, find the tutorials on our Home page.
What is PHP?
* PHP stands for PHP: Hypertext Preprocessor
* PHP is a server-side scripting language, like ASP
* PHP scripts are executed on the server
* PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
* PHP is an open source software
* PHP is free to download and use
What is a PHP File ?
* PHP files can contain text, HTML tags and scripts
* PHP files are returned to the browser as plain HTML
* PHP files have a file extension of ".php", ".php3", or ".phtml"
What is MySQL ?
* MySQL is a database server
* MySQL is ideal for both small and large applications
* MySQL supports standard SQL
* MySQL compiles on a number of platforms
* MySQL is free to download and use
PHP + MySQL
* PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)
Why PHP?
* PHP runs on different platforms (Windows, Linux, Unix, etc.)
* PHP is compatible with almost all servers used today (Apache, IIS, etc.)
* PHP is FREE to download from the official PHP resource: www.php.net
* PHP is easy to learn and runs efficiently on the server side
Where to Start ?
To get access to a web server with PHP support, you can:
* Install Apache (or IIS) on your own server, install PHP, and MySQL
* Or find a web hosting plan with PHP and MySQL support.
What do you Need?
If your server supports PHP you don't need to do anything.
Just create some .php files in your web directory, and the server will parse them for you. Because it is free, most web hosts offer PHP support.
However, if your server does not support PHP, you must install PHP.
Here is a link to a good tutorial from PHP.net on how to install PHP5: http://www.php.net/manual/en/install.php
Download PHP
Download PHP for free here: http://www.php.net/downloads.php
Download MySQL Database
Download MySQL for free here: http://www.mysql.com/downloads/
Download Apache Server
Download Apache for free here: http://httpd.apache.org/download.cgi
PHP 5.4 alpha1 released[28-Jun-2011]
The PHP development team is proud to announce the first PHP 5.4 alpha release. PHP 5.4 includes new language features and removes several legacy (deprecated) behaviors. Read the NEWS file for a complete list of changes.
THIS IS A DEVELOPMENT PREVIEW - DO NOT USE IT IN PRODUCTION!
This alpha release exists to encourage users to identify bugs, and to ensure that all new features and backward compatibility breaks are evaluated and documented before PHP 5.4.0 is released. Please report findings to the QA mailing list and/or the PHP bug tracker. Windows binaries can be downloaded from the Windows QA site.
Here is an incomplete list of changes:
* Added: Traits language construct
* Added: Array dereferencing support
* Added: DTrace support
* Improved: Improved Zend Engine memory usage and performance
* Moved: ext/sqlite moved to pecl (sqlite3 support is still built-in)
Please note that some legacy features have been removed, including:
* Removed: break/continue $var syntax
* Removed: register_globals, allow_call_time_pass_reference, and register_long_arrays ini options
* Removed: session_is_regisitered(), session_registered(), and session_unregister()
This is the first release that adopts the releaseprocess RFC. The next alpha will be released within four weeks. The PHP 5.4 feature set and API has not been finalized.
PHP Documentation update
[25-Jun-2011]
PHP has several new documentation features that the community should be aware of:
* pman - PHP man pages
o $ pear install doc.php.net/pman
o $ pman strlen (this example displays a local textual version of the strlen
docs)
* Enhanced CHM - contains user notes (over 25,000)
o This additional CHM file is downloadable
* Online Documentation Editor - allows everyone to edit the PHP manual
o URL: https://edit.php.net/
o Every manual page will link to it in the future
o Includes an IRC window to the #php.doc channel, so let's talk
We hope you find the above features useful, and please write phpdoc@lists.php.net with feedback. Additional features are being refined, which includes a JSON version of the manual.
php.net security notice
[19-Mar-2011]
The wiki.php.net box was compromised and the attackers were able to collect wiki account credentials. No other machines in the php.net infrastructure appear to have been affected. Our biggest concern is, of course, the integrity of our source code. We did an extensive code audit and looked at every commit since 5.3.5 to make sure that no stolen accounts were used to inject anything malicious. Nothing was found. The compromised machine has been wiped and we are forcing a password change for all svn accounts.
We are still investigating the details of the attack which combined a vulnerability in the Wiki software with a Linux root exploit.
PHP 5.3.6 Released!
[17-Mar-2011]
The PHP development team would like to announce the immediate availability of PHP 5.3.6. This release focuses on improving the stability of the PHP 5.3.x branch with over 60 bug fixes, some of which are security related.
Security Enhancements and Fixes in PHP 5.3.6:
* Enforce security in the fastcgi protocol parsing with fpm SAPI.
* Fixed bug #54247 (format-string vulnerability on Phar). (CVE-2011-1153)
* Fixed bug #54193 (Integer overflow in shmop_read()). (CVE-2011-1092)
* Fixed bug #54055 (buffer overrun with high values for precision ini setting).
* Fixed bug #54002 (crash on crafted tag in exif). (CVE-2011-0708)
* Fixed bug #53885 (ZipArchive segfault with FL_UNCHANGED on empty archive). (CVE-2011-0421)
Key enhancements in PHP 5.3.6 include:
* Upgraded bundled Sqlite3 to version 3.7.4.
* Upgraded bundled PCRE to version 8.11.
* Added ability to connect to HTTPS sites through proxy with basic authentication using stream_context/http/header/Proxy-Authorization.
* Added options to debug backtrace functions.
* Changed default value of ini directive serialize_precision from 100 to 17.
* Fixed Bug #53971 (isset() and empty() produce apparently spurious runtime error).
* Fixed Bug #53958 (Closures can't 'use' shared variables by value and by reference).
* Fixed bug #53577 (Regression introduced in 5.3.4 in open_basedir with a trailing forward slash).
* Over 60 other bug fixes.
Windows users: please mind that we do no longer provide builds created with Visual Studio C++ 6. It is impossible to maintain a high quality and safe build of PHP for Windows using this unmaintained compiler.
For Apache SAPIs (php5_apache2_2.dll), be sure that you use a Visual Studio C++ 9 version of Apache. We recommend the Apache builds as provided by ApacheLounge. For any other SAPI (CLI, FastCGI via mod_fcgi, FastCGI with IIS or other FastCGI capable server), everything works as before. Third party extension providers must rebuild their extensions to make them compatible and loadable with the Visual Studio C++9 builds that we now provide.
All PHP users should note that the PHP 5.2 series is NOT supported anymore. All users are strongly encouraged to upgrade to PHP 5.3.6.
For a full list of changes in PHP 5.3.6, see the ChangeLog. For source downloads please visit our downloads page, Windows binaries can be found on windows.php.net/download/.
PHP 5.3.5 and 5.2.17 Released!
[06-Jan-2011]
The PHP development team would like to announce the immediate availability of PHP 5.3.5 and 5.2.17.
This release resolves a critical issue, reported as PHP bug #53632 and CVE-2010-4645, where conversions from string to double might cause the PHP interpreter to hang on systems using x87 FPU registers.
The problem is known to only affect x86 32-bit PHP processes, regardless of whether the system hosting PHP is 32-bit or 64-bit. You can test whether your system is affected by running this script from the command line.
All users of PHP are strongly advised to update to these versions immediately.
Troubleshooting
When running your scripts, if you receive a 500 Internal Server Error message, please check the following:
1. Check to make sure the file permission is set to 700 (read, write, and execute for owner only).
2. Check the Directory (folder) permission. Setting it to 777 will give you a 500 - Internal Server Error.
Set it to 711.
3. When the file/directory permissions are set correctly, and you are still getting a 500 error, it is likely due to line compatibility between the program used to create/upload the file and the Linux server. To correct this, login to the account on Webserve via an SSH client that provides a command line interface and do the following:
1. Move to the directory where your php file is using the 'cd' command (e.g., cd www).
2. Open the file using an editor such as nano and save it by typing Ctrl + o
This simple steps may solve your problem because it converts Windows CR/LF (carriage return/line feed) to Unix LF (line feed) or Mac CR to Unix LF.
To prevent this problem, you need to change settings/preferences in the program you are using to develop php files. To set preferences in Dreamweaver, go to Edit => Preferences. Select Code Format or HTML Format (depending on the versions). Change Line Breaks to LF (Unix).
Sample PHP Code
Place a file containing the code below inside the www directory of your Webserve account and save it as info.php
<*?php
phpinfo();
?*>
To view the file, you would then provide the web site address to the file. For example:
http://www.indiana.edu/~myaccount/info.php
It will output information about the current state of PHP, which includes information about PHP compilation options and extensions, the PHP version, server information and environment (if compiled as a module), the PHP environment, OS version information, paths, master and local values of configuration options, HTTP headers, and the PHP License. Because every system is setup differently, phpinfo() can be used to check configuration settings and for available predefined variables on a given system . For more information see php.net's entry for phpinfo.
Increasing the Memory Limit
You can increase the memory limit with a php.ini file in the same directory as your php script. If you have multiple subdirectories, you will need to place the php.ini file in each subdirectory that has php scripts.
In that php.ini file you can specify the memory_limit you wish to set by using:
memory_limit = whatever integer you wish
You can verify the memory_limit has changed by creating a php info file.
Comments in PHP
In PHP, we use // to make a single-line comment or /* and */ to make a large comment block.
Variables in PHP
Variables are used for storing values, like text strings, numbers or arrays.
When a variable is declared, it can be used over and over again in your script.
All variables in PHP start with a $ sign symbol.
The correct way of declaring a variable in PHP:
PHP is a Loosely Typed Language
In PHP, a variable does not need to be declared before adding a value to it.
In the example above, you see that you do not have to tell PHP which data type the variable is.
PHP automatically converts the variable to the correct data type, depending on its value.
In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.
In PHP, the variable is declared automatically when you use it.
Naming Rules for Variables
* A variable name must start with a letter or an underscore "_"
* A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
* A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($*my_string), or with capitalization ($*myString).