Languages - PHP
Basics

PHP is a scripting language developed specifically to produce web pages - the expected output is html. As a result, many php files contain both program code and html.

Generic info - Basics | Arrays | Functions | if statements | Objects


Code

PHP pages contain a mixture of HTML and PHP code. By default, everything in the file is HTML unless it is located between special tags. Details of using the echo are covered at Debug.html.


Comments

PHP files may have both PHP and HTML comments.


Variables

All variables are variants and start with a dollar sign ($). Constants are named with all uppercase letters and used without a dollar sign.


Functions

All variables used inside a function are local - by default. There are 2 exceptions In the following example (from Debug.html - see that for an explanation), show the basic structure. To modify one of the passed parameters, it must be passed by reference (place an ampersand in front of the name). For additional examples, see Functions.


Strings

Either single quotes or double quotes can be used to define a string. When single quotes are used, the resultant string is exactly what is typed.

However, with double quotes, most (not all) variables present in the string will be evaluated and the result placed in the string.

The following example shows 3 ways to mix a variable with a string

Inline substitution requires double quotes. To stop variable substitution, either

Some additional string functions

In query strings, back ticks are required for field names and many other places - be careful, even though they look similar, these are not the same as single quotes.


Include other files

There are 4 different methods to include one PHP file in another. In general, the library files which contain code should be required and included only once. As the example shows, these are usually placed in a subdirectory. I suggest having one directory for library files you use in multiple projects, and a separate directory for those that are specific to the current project.


try .. catch

Exceptions Built-in Exceptions Types


Author: Robert Clemenzi
URL: http:// mc-computing.com / Languages / PHP / Basics.html