Syntax
General
PHP entries must end with a semicolon.
Quotes
Use the opposite of what you want to appear:
echo 'She said, "How are you?"' = She said, "How are you?"
echo "She said, 'How are you?'" = She said, 'How are you?'
Or, escape the character with a backslash.
Line breaks
This sentence is
printed over two lines. (There is a CRLF in the php file, but not in the browser.)
The CRLF shows up in View->Source.
Use br in the file to affect the rendered page the user sees.
There are three places to consider:
- In the source php file.
- In the View->Source html sent to the browser.
- What the viewer sees in the browser.
You can put \n in the php source file to cause the View->Source to have a line feed (but the user won't see it). At least the source looks good!
Comments
# This is a php comment
// This is a php comment
/* This is a multiline comment */
Variables
Rules
Names must start with a dollar sign.
The first letter after the dollar sign must be a letter or underscore.
Names ARE case sensitive.
Can be printed as print $some_var (no quotes), or print "Hello, $name" (double quotes), but not print 'Hello, $name' (single quotes).
String concatenation is done with a period: $city . ', ' . $state = Lexington, KY
Strings
$str = "This is a string"
echo $str = This is a string
strlen($str) = 16
strtolower($str) = this is a string
strtoupper($str) = THIS IS A STRING
$str .= " and more" = This is a string and more
Values in single quotes are treated literally, values in double quotes are interpreted. If $var="test"
Literal: echo 'var is equal to $var' = var is equal to $var
Interpreted: echo "var is equal to $var" = var is equal to test
Numbers
$n = 123456.789;
round($n) = 123457
round($n, 2) = 123456.79
number_format($n) = 123,457
number_format($n, 2) = 123,456.79
Constants
define ('PI', 3.141592763); echo PI; = 3.141592763
Constants can't be included in quotes. They must get their own echo.
Some are predefined:
echo PHP_VERSION; = 7.1.33
echo PHP_OS; = Linux
Conditions
The following are true
$var, if $var has a value other than 0, an empty string, FALSE, or NULL
isset($var), if $var has any value other than NULL, including 0, FALSE, or an empty string
TRUE, true, True, etc.
empty($var), if $var is an empty string, 0, NULL, or FALSE
Examples
variable |
value |
isset() |
empty() |
== TRUE |
== FALSE |
$str_not_empty |
"This string is not empty" |
1 |
|
1 |
|
$str_empty |
"" |
1 |
1 |
|
1 |
$str_not_defined |
|
|
1 |
|
1 |
$str_NULL |
NULL |
|
1 |
|
1 |
$val_zero |
0 |
1 |
1 |
|
1 |
$val_one |
1 |
1 |
|
1 |
|
$bool_TRUE |
TRUE |
1 |
|
1 |
|
$bool_true |
true |
1 |
|
1 |
|
$bool_false |
false |
1 |
1 |
|
1 |
$_COOKIE['ABCDE'] |
a non existent cookie |
|
1 |
|
1 |
Operators
== is equal to
!= is not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
! not
&& and
|| or
XOR and not
Control
Loops
for ($i=1; $i<=10; $i++) echo $i . " ";
1 2 3 4 5 6 7 8 9 10
Arrays
Rules
Arrays elements can be arrays themselves.
May be indexed by numbers, or by strings.
Arrays can be turned into strings (explode). Strings can be turned into arrays (implode).
Arrays can be sorted on key, value (maintaining keys or not). See sort, asort, ksort
Examples
$band[] = "element0"; $band[] = "element1"; $band[] = "element2"; echo $band[1]; =
element1
$states = array('KY' => "Kentucky", 'TN' => "Tenn"); echo $states['TN']; =
Tenn
$days = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'); echo $days[6]; =
Sun
$days = array(3 => 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'); echo $days[6]; =
Thu
foreach ($days as $k => $val) echo "The value at $k is $val."
The value at 3 is Mon.
The value at 4 is Tue.
The value at 5 is Wed.
The value at 6 is Thu.
The value at 7 is Fri.
The value at 8 is Sat.
The value at 9 is Sun.
$dozen = range(1,12); foreach ($dozen as $k => $v)
1 2 3 4 5 6 7 8 9 10 11 12
Superglobal arrays
$_GET
$_POST
$_REQUEST
$_SERVER
USER = apache
HOME = /usr/share/httpd
SCRIPT_NAME = /php.php
REQUEST_URI = /php.php
QUERY_STRING =
REQUEST_METHOD = GET
SERVER_PROTOCOL = HTTP/1.1
GATEWAY_INTERFACE = CGI/1.1
REMOTE_PORT = 45600
SCRIPT_FILENAME = /home/vhosts/phpman.freevar.com/php.php
SERVER_ADMIN = contact@freewha.com
CONTEXT_DOCUMENT_ROOT = /home/vhosts/phpman.freevar.com
CONTEXT_PREFIX =
REQUEST_SCHEME = http
DOCUMENT_ROOT = /home/vhosts/phpman.freevar.com
REMOTE_ADDR = 3.129.25.184
SERVER_PORT = 80
SERVER_ADDR = 69.30.201.252
SERVER_NAME = phpman.freevar.com
SERVER_SOFTWARE = Apache/2.4.51
SERVER_SIGNATURE =
PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
HTTP_HOST = phpman.freevar.com
HTTP_ACCEPT_ENCODING = gzip, br, zstd, deflate
HTTP_USER_AGENT = Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
HTTP_ACCEPT = */*
proxy-nokeepalive = 1
SCRIPT_URI = http://phpman.freevar.com/php.php
SCRIPT_URL = /php.php
UNIQUE_ID = Z6NA7WuSmdPA2_gfw-RgjAAAAEg
FCGI_ROLE = RESPONDER
PHP_SELF = /php.php
REQUEST_TIME_FLOAT = 1738752237.741
REQUEST_TIME = 1738752237
$_ENV
$_SESSION
Warning: Invalid argument supplied for foreach() in /home/vhosts/phpman.freevar.com/php.php on line 386
$_COOKIE
Miscellaneous
Cookies
Set a cookie: setcookie("user", "Alex Porter", time()+3600);
Delete a cookie: setcookie("user");
Delete a cookie (expire in the past): setcookie("user", "", time()-3600);
Read a cookie: $_COOKIE["user"];
Multiple files
include() - failure results in a warning, script continues
include_once()
require() - failre results in a warning, script fails
require_once()
_once() guarantees the file is included at most one time.