Using PHP
PHP is a server-side language. That is, PHP files are
processed through a PHP Web server.
PHP code > processed through PHP parser (or engine) > returned to client > interpreted by browser > viewed by user
PHP files may include some or all of the following elements:
- Introduction: overview, comments, statements, output
- variables: reserved memory locations that hold (changing) values
- arrays: reserved memory locations that hold multiple (changing) values
- conditional statements: or decision-making statements (if...else)
- loops: repetitive tasks
- functions: predefined tasks
Example: isset() vs empty()
- strings
- forms
- includes
- query strings
- sessions
- debugging
All PHP code must be surrounded by the following PHP tags:
<?php
... PHP code ...
?>
All PHP statements *must* end with semicolon (;):
<?php
do something;
?>
Comments
Use comments to internally document code that cannot be seen by the user. (Also, good for debugging!)
Three Methods:
- Single-line comment: pound sign (#), Unix shell-style or Perl style
- Single-line comment: two forward slashes (//), C-Style
- Multi-line comment: (/* ... */), C-Style
Note: C-style comments end at the first */ encountered.
Make sure you don't nest C-style comments.
/*
echo 'Hello World'; /* This comment will cause error. */
*/
Output
There are various ways to output values.
Both print() and echo() are not actual functions (but, rather language constructs) so, parentheses are not required.
Also, sprintf() returns a string while printf() outputs a string (to standard output):
<?php
echo("Text here");
//is equivalent to:
echo "Text here";
print("Text here");
//is equivalent to:
print "Text here";
printf("Text here");
//is equivalent to:
echo sprintf("Text here");
?>
Text here
Text here
Text here
Text here
Text here
Text here
Naming variables:
- begin with dollar sign($)
- cannot be number
- no spaces or special characters, except underscore (_)
- case-sensitive ($myVar different than $MyVar)
Note: It is not necessary to initialize
variables in PHP; however it is a very good
practice. Uninitialized variables have a default value of
their type depending on the context in which they are used - booleans
default to FALSE, integers and floats default to zero, strings (e.g.,
used in echo()) are set to an empty string, and arrays to an empty array.
http://www.php.net/manual/en/language.variables.basics.php
Assigning Values to Variables:
<?php
$myVar = 10;
echo $myVar;
$yourVar = "Hello World";
echo $yourVar;
/*
Variables that have no value assigned are of type null.
The size of null is 0 bytes.
Assigning null to a variable deletes the contents of that variable from memory.
However, unset() function removes the variable, as if it were never
declared (see error below, with error_reporting turned on.)
*/
$yourVar = null; //removes contents of variable
unset($yourVar); //removes variable from memory
//addition returns 10
echo 5 + "5";
//concatenation (dot) operator returns 55
echo 5 . 5;
?>
10
Hello World
10
55
var_dump():
var_dump() is a very useful PHP function when debugging, and/or tracking variables.
In addition, var_dump() function displays variable details, including contents.
It will accept any expression. After processing the result will be displayed.
In addition, multiple arguments may be passed to the function by separating each with a comma (,).
<?php
$myVar = 15;
echo $myVar;
$yourVar = "Hello Universe";
echo $yourVar;
var_dump($myVar, $yourVar);
$myVar = null; //removes contents of variable
unset($yourVar); //removes variable from memory
var_dump($myVar, $yourVar); //will generate error, with error_reporting turned on.
?>
15
Hello Universe
int(15)
string(14) "Hello Universe"
Warning: Undefined variable $yourVar in D:\InetPub\vhosts\mjowett-1638.package\qcitr.com\wwwroot\demos\index.php on line 267
NULL
NULL
Converting Special Characters to HTML Entities:
Certain characters have special significance in HTML,
and should be represented by HTML entities if they are to preserve
their meanings. For example:
Some predefined characters:
& (ampersand) becomes &
" (double quote) becomes "
' (single quote) becomes '
< (less than) becomes <
> (greater than) becomes >
The following two functions return a string with these
conversions made:
Using htmlentities() or
htmlspecialchars():
The htmlspecialchars() and htmlentities() functions convert predefined
characters to HTML entities. Depending upon the charset used, these
conversions are necessary in order to be displayed w/in a browser, or used
w/in XML.
Note: The conversions *must* be viewed
using the "View Source" command.
htmlspecialchars(): Convert special characters to HTML entities.
Example:
$specialchars = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $specialchars;
// view source returns: <a href='test'>Test</a>
Result:<a href='test'>Test</a>
no htmlspecialchars() -- that is, no conversion of special characters
$no_specialchars = "<a href='test'>Test</a>";
echo $no_specialchars;
Result:Test
htmlentities(): Convert *
ALL* applicable characters to HTML entities.
Identical to htmlspecialchars() in all ways, except with htmlentities(),
*ALL* characters which have HTML character entity equivalents are
translated into these entities.
Example:
$str = "A 'quotation' may be <b>bold</b>.";
echo htmlentities($str);
// Or, to handle code as XHTML, and encode in utf-8 charset
echo htmlentities($str, ENT_QUOTES | 'ENT_XHTML', "UTF-8");
Result:A 'quotation' may be <b>bold</b>.
Fatal error: Uncaught TypeError: Unsupported operand types: int | string in D:\InetPub\vhosts\mjowett-1638.package\qcitr.com\wwwroot\demos\index.php:354
Stack trace:
#0 {main}
thrown in
D:\InetPub\vhosts\mjowett-1638.package\qcitr.com\wwwroot\demos\index.php on line
354