THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP Examples


PHP Syntax

Write text to the output using PHP
Add comments in PHP
Keywords, classes, functions, and user-defined functions ARE NOT case-sensitive
Variable names ARE case-sensitive

Examples explained


PHP Variables

Create different variables
Test global scope (variable outside function)
Test local scope (variable inside function)
Use the global keyword to access a global variable from within a function
Use the $GLOBALS[] array to access a global variable from within a function
Use the static keyword to let a local variable not be deleted after execution of function

Examples explained


PHP Echo and Print

Display strings with the echo command
Display strings and variables with the echo command
Display strings with the print command
Display strings and variables with the print command

Examples explained


PHP Data Types

PHP string
PHP integer
PHP float
PHP array
PHP object
PHP NULL value

Examples explained


PHP Strings

Get the length of a string - strlen()
Count the number of words in a string - str_word_count()
Reverse a string - strrev()
Search for a specific text within a string - strpos()
Replace text within a string - str_replace()

Examples explained


PHP Constants

Case-sensitive constant name
Case-insensitive constant name

Examples explained


PHP Operators

Arithmetic operator: Addition (+)
Arithmetic operator: Subtraction (-)
Arithmetic operator: Multiplication (*)
Arithmetic operator: Division (/)
Arithmetic operator: Modulus (%)
Assignment operator: x = y
Assignment operator: x += y
Assignment operator: x -= y
Assignment operator: x *= y
Assignment operator: x /= y
Assignment operator: x %= y
Comparison operator: Equal (==)
Comparison operator: Identical (===)
Comparison operator: Not equal (!=)
Comparison operator: Not equal (<>)
Comparison operator: Not identical (!==)
Comparison operator: Greater than (>)
Comparison operator: Less than (<)
Comparison operator: Greater than or equal (>=)
Comparison operator: Less than or equal (<=)
Increment operator: ++$x
Increment operator: $x++
Decrement operator: --$x
Decrement operator: $x--
Logical operator: and
Logical operator: or
Logical operator: xor
Logical operator: && (and)
Logical operator: || (or)
Logical operator: not
String operator: Concatenation of $txt1 and $txt2
String operator: Appends $txt2 to $txt1
Array operator: Union (+)
Array operator: Equality (==)
Array operator: Identity (===)
Array operator: Inequality (!=)
Array operator: Inequality (<>)
Array operator: Non-identity (!==)

Examples explained


PHP If...Else and Switch Statements

The if statement
The if...else statement
The if...elseif...else statement
The switch statement

Examples explained


PHP While and For Loops

The while loop
The do...while loop
Another do...while loop
The for loop
The foreach loop

Examples explained


PHP Functions

Create a function
Function with one argument
Function with two arguments
Function with default argument value
Function that returns a value

Examples explained


PHP Arrays

Indexed arrays
count() - Return the length of an array
Loop through an indexed array
Associative arrays
Loop through an associative array

Examples explained


PHP Sorting Arrays

sort() - Sort array in ascending alphabetical order
sort() - Sort array in ascending numerical order
rsort() - Sort array in descending alphabetical order
rsort() - Sort array in descending numerical order
asort() - Sort array in ascending order, according to value
ksort() - Sort array in ascending order, according to key
arsort() - Sort array in descending order, according to value
krsort() - Sort array in descending order, according to key

Examples explained


PHP Superglobals

$GLOBAL - Used to access global variables from anywhere in the PHP script
$_SERVER - Holds information about headers, paths, and script locations
$_REQUEST - Used to collect data after submitting an HTML form
$_POST - Used to collect form data after submitting an HTML form. Also used to pass variables
$_GET - Collect data sent in the URL

Examples explained


PHP Form Validation

PHP Form Validation

Example explained


PHP Multidimensional Arrays

Output elements from a multidimensional array
Loop through a multidimensional array

Examples explained


PHP Date and Time

Format today's date in several ways
Automatically update the copyright year on your website
Output the current time (server time)
Set timezone, then output current time
Create a date and time from a number of parameters in mktime()
Create a date and time from the strtotime() function
Create more dates/times from strtotime()
Output the dates for the next six Saturdays
Output the number of days until 4th of July

Examples explained


PHP Include Files

Use include to include "footer.php" in a page
Use include to include "menu.php" in a page
Use include to include "vars.php" in a page
Use include to include a non-existing file
Use require to include a non-existing file

Examples explained


PHP File Handling

Use readfile() to read a file and write it to the output buffer

Examples explained


PHP File Open/Read/Close

Use fopen(), fread(), and fclose() to open, read, and close a file
Use fgets() to read a single line from a file
Use feof() to read through a file, line by line, until end-of-file is reached
Use fgetc() to read a single character from a file

Examples explained


PHP Cookies

Create and retrieve a cookie
Modify a cookie value
Delete a cookie
Check if cookies are enabled

Examples explained


PHP Sessions

Start a session
Get session variable values
Get all session variable values
Modify a session variable
Destroy a session

Examples explained


PHP Filters

Use filter_list() to list what the PHP filter extension offers
Sanitize a string
Validate an integer
Validate an integer that is 0
Validate an IP address
Sanitize and validate an email address
Sanitize and validate a URL

Examples explained


PHP Select Data From MySQL

Select data with MySQLi (Object-oriented)
Select data with MySQLi (Object-oriented) and put result in an HTML table
Select data with MySQLi (Procedural)
Select data with PDO (+ Prepared statements)

Examples explained


PHP SimpleXML Parser

Use simplexml_load_string() to read XML data from a string
Use simplexml_load_file() to read XML data from a file
Get node values
Get node values of specific elements
Get node values - loop
Get attribute values
Get attribute values - loop

Examples explained


PHP XML Expat Parser

Initialize an XML Expat parser, define some handlers, then parse an XML file

Examples explained