PHP is an acronym for PHP: Hypertext Preprocessor.
It is an open-source scripting language that is mostly used for web development.
The PHP script (code) can be executed from the command line, but also on the server (in the case of web development).
PHP files have extension .php
.
Aside of the PHP code, PHP files may also contain text, HTML, CSS, and JavaScript code.
A PHP script can be placed anywhere in the HTML document.
A PHP script starts with A PHP script starts with <?php
and ends with ?>
.
Scripting languages are usually interpreted at runtime rather than compiled. A script only runs in response to an event. It has little, or no user interaction after that initial event so a PHP script does not run until a web page is requested. Then it launches, follows its instructions from top to bottom and quits until another action launches the script again.
A program runs even when not responding to events. It continues to run and to wait for an interaction whether that interaction comes from a user making choices, or from other programs or input.
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment
*/
/*
* This multiple-lines comment looks even better
*/
Variable names have to begin with a dollar sign ($
).
The dollar sign must be followed by a letter or an underscore. The remainder can be any combination of letters, numbers, and underscores (A-z
, 0-9
, and _
).
The variable name is case-sensitive, so $username
is a different variable than $UserName
.
There are different ways to separate words in variable names. The large majority of developers tend to use the camelCase notation. Most PHP developers tend to use_underscores. CSS property names use-dashes.
TRUE
or FALSE
1
, -13
, 131313
13.13
Hello world
”, or “13
”PHP is a loosely typed language – you do not have to define which data type the variable is.
PHP recognizes the type of data being stored into a variable at the time it is being assigned, be that a string, a number, etc.
$firstName = "Juraj";
$lastName = "Stefanic";
$year = 2013;
$number = 13;
When working with strings, you can enclose literals in either single or double quotes. This allows you to enclose one kind of quote inside another.
$q = 'T.A. Edison: "There is time for everything"';
PHP substitutes a variable name within a double-quoted string with that variable's value. Variable substitution is not being performed within the single quotes.
$quote= "There is time for everything";
echo "My favorite quote is $quote.";
Output: My favorite quote is "There is time for everything".
$quote= "There is time for everything";
echo 'My favorite quote is $quote.
Output: My favorite show is $quote.
Variables can be declared anywhere in the script.
Recall: The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has three different variable scopes: local, global, static.
A variable declared outside a function has a GLOBAL SCOPE - can only be accessed outside a function.
A variable declared within a function has a LOCAL SCOPE - can only be accessed within that function.
A STATIC VARIABLE is a variable that exists only in a local function scope, and it does not lose its value when that function is completed.
A constant is an identifier/name for a scalar value.
Constants are automatically global and can be used across the entire script.
By convention, constant identifiers are always uppercase.
A constant name should start with a letter or underscore, followed by any number of letters, numbers, or underscores.
To create a constant, use the define() function.
<?php
define("Greeting", "Welcome");
echo
?>
When using a constant, there is NO ($
) sign.
As of PHP 5.3.0, you can define a constant by using the the const
keyword (const keyword was used for class constants).
As of PHP 5.6 it is possible to define array constant using const
keywords and as of PHP 7 array constants can also be defined using define()
.
<?php
const HELLO = 'Hello World';
echo HELLO;
?>
The value of a constant cannot change during the execution of the script except for magic constants.
Arrays are known as compound data types. What it means is that they are more complex in structure than simple scalar data types such as strings and integers.
In PHP, there are three types of arrays:
Superglobals are built-in variables, usually arrays, that are always available in all scopes.
$_POST
and $_GET
belong to this specific group of variables.
$GLOBALS
is an associative array that references all variables available in global scope.
Other superglobals: $_SERVER
, $_FILES
, $_COOKIE
, $_SESSION
, $_REQUEST
, $_ENV
.
Functions are defined using the reserved word function followed by a unique name that should begin with a letter or an underscore character, followed by any number of letters, underscores, or numbers.
Round brackets () that follow the function name are used to pass in any parameters. Curly braces {} are used to surround any code that is to be contained within the function.
function saySomething($string) {
echo $string;
}
PHP supports passing arguments by value (the default), passing arguments by reference, and default argument values.
It is possible to import and evaluate the content of an external PHP file into your current script. There are four statements:
include 'externalFile.php'
OR
include ('externalFile.php');
Included files inherit the variable scope of the include's location.
Included files drop out of PHP parsing mode and into HTML mode.
After the file is included, PHP parsing mode is restored.
Included files do not have to have any particular extension (common practice: .inc
, OR .php
).
Include files do not have to be on the same server, and can be fetched with http.