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.
- All php commands are between <?php and ?>
- All php commands must end with a semicolon
- Code blocks (used with if and while statements) begin and end with curly braces
Everything is html until the following
now more html
New paragraph";
if ($a == 5){
echo " the condition was true";
echo "\n The if statement condition must be in parentheses";
echo "\n Curly braces define the begin and end of a block of code ";
}
?>
|
Details of using the echo are covered at
Debug.html.
Comments
PHP files may have both PHP and HTML comments.
PHP Comments | HTML Comments
|
---|
// single line comment
# a single line comment
/*
block comment
*/
|
<! simple comment - can not contain other tags >
<!--
The only officially allowed comment
Can contain <b>various tags</b>
-->
|
Variables
All variables are variants and start with a dollar sign ($).
$a = "some string"; // basic assignment statement
echo a$; // include the string in the output
$a = 5; // $a can be a string, number, array, anything
|
Constants are named with all uppercase letters
and used without a dollar sign.
define ("DB_SERVER", "localhost");
define ("DB_USER", "read_only");
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
|
Functions
All variables used inside a function are local - by default.
There are 2 exceptions
- Those passed as function parameters
- Those declared to be global
In the following example (from
Debug.html - see that for an explanation),
show the basic structure.
function mc_generic_function($input_string){
global $debug;
global $debug_str;
$a = "Start of mc_generic_function(\$input_string)\n";
$debug_str .= " $a"; // add entry to trace string
if ($debug) {echo "$a";} // show it right now
// the rest of the function goes here
return = "some result"; // the return type is a variant - be careful
}
|
To modify one of the passed parameters, it must be
passed by reference
(place an ampersand in front of the name).
function mc_generic_function(&$read_write_string){
$read_write_string = "some result"; // the new value will be available outside the function
}
|
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
- The first one uses a period for concatenation
- The second places the variable inside the double quotes
- The third also adds curly braces around the variable
echo $variable . " more text"; // concatenation - via a period
echo "$variable more text"; // concatenation - via variable substitution
echo "{$variable} more text"; // concatenation, makes the variable clear
// useful if there is no trailing space
// variable substitution does not work with single quotes
|
Inline substitution requires double quotes.
To stop variable substitution, either
- Place a backslash in front of the dollar sign, or
- Use single quotes
echo "\$variable more text"; // escaped $ -> $variable more text
echo '$variable more text'; // single quotes -> $variable more text
|
Some additional string functions
strlen($s)
trim($s) remove white space
substr($s,$start,$len) return substring
strpos
str_replace($a,$b,$s) in $s, replace $a with $b
|
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.
<?php require_once("includes/xyz.php"); ?>
include Keep running if not found
require Stop rendering the page if not found
include_once Same as above, but only one time
require_once
|
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
try{
// try something here
}catch (Exception $e){
// this runs when there is a problem
}
|
Built-in Exceptions Types
Author:
Robert Clemenzi