echo has no return value while print has a return value of 1 so it can be used in expressions.
echo can take multiple parameters (although such usage is rare) while print can take one argument.
strlen() function returns the length of a string.
str_word_count() function counts the number of words in a string
strpos() function searches for a specific text within a string.
str_replace() function replaces some characters with some other characters in a string.
To create a constant, use the define() function.
define(name, value, case-insensitive)
Constants are automatically global and can be used across the entire script.
+ is Addition / Array Union
. is String Concatenation
foreach Loop
foreach ($array as $value) {
code to be executed;
}
PHP Default Argument Value
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
In PHP, there are three types of arrays:
Indexed arrays - Arrays with a numeric index
Associative arrays - Arrays with named keys
Multidimensional arrays - Arrays containing one or more arrays
Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array:
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
or:
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
Superglobals
$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods).
$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.
$_REQUEST is used to collect data after submitting an HTML form.
$_POST is widely used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.
$_GET can also be used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL.