PHP is a common computing programme. It is an alternative to ASP.NET, JSP, ColdFusion, and Ruby on Rails.
PHP was invented by Rasmus Lerdorf in 1994. It was originally conceived as a simple programme to track the visitors to his online biography, so he gave it the innocuous and now totally inappropriate name of 'Personal Home Page'!
As it was adapted to a far greater range of applications, it was renamed 'PHP: Hypertext PreProcessor' (ok, not perfect as acronyms go, but close enough!).
PHP is defined by the PHP website (www.php.net) as: "a widely used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML."
PHP is a server-side scripting language. This means that PHP is inserted within an HTML page, but acts on the server. To use PHP, it is necessary to have access to a server, which may be a remote computer, which is the internet access gateway, or a local server on the same computer, using a server application like Apache.
If a file contains any PHP, it needs to have the extension
For example, your first PHP file could be called:
myfirstphpfile.php, and would be placed wherever you need it on the site.
PHP code that is used by more than one page could be placed in a separate file and folder, and included in the HTML script when needed.
PHP is embedded within an HTML file. Special tags are used to identify the PHP script. These are:
PHP has three ways of including comments: two for a single line, and one for a comment that stretches over multiple lines:
PHP uses statements which must conclude with a semi-colon (;):
Variables are exactly as their name suggests: variable. If a quantity is not a variable (cannot - or should not - change), then it is a constant (always the same).
To define a constant:
To define a variable in PHP, use a dollar sign ($). e.g. the variable which is the name of a customer being called from a database of many hundreds of customers could be defined as:
Variables can be defined (or redefined) at any point in a script. They can also be added together: for example, the full name of a customer could take the two parts of the name and combine them:
$fullname = $firstname.$lastname
A problem with this example is that the two name parts will have no space between them. The following example demonstrates how concatenation may be used to include non-variable elements, such as a space or a line break (<br />), into a variable:
The output of this code is
Fred McGurty, and
Variables are case sensitive and can include the underscore. These variable names:
$_customer, are all valid and refer to different stored values.
Variable names may include numbers, but the first character after the dollar sign ($) must be a letter or an underscore (_).
$_variable1 are valid, but
$1variable is not.
For clarity, a common technique to make variable names with multiple words more readible is to use the 'camelCase'. Instead of making a variable for 'my next great idea'
$mynextgreatidea, we use the camel case to make it more legible:
The equals sign (=) is used to assign a value to a variable. If the variable does not exist, the assignment also creates a new variable with this name. If the variable exists and has a previous value, the assignment replaces the previous value with the new value in the assignment statement. For example:
The output will be
The $oldVariable value (42) is replaced by the new value (21). In this example, a new variable is then created and given the same value as the old variable.
Now, examine the difference between these two
The outputs are:
The value stored in the newly created variable $newVariable is 21
The value stored in the newly created variable 21 is 21
Notice that when the variable name is inside single quote marks (' ') in the
echo statement, the script prints out the string literally, even though it is a variable name. Everything inside the quote marks of an echo statement is considered a string.
If, instead, double quote marks (" ") are used in the
echo statement, the variable name is interpreted as a variable and its value printed out instead of the string.
Single quote marks will print literally what you write.
Use double quote marks if you want to print the value of the variable instead.
Variables can be different types. Specifying the type can enable the programme to carry out certain operations. The two main types are string and number.
A string is simply a series of alphanumeric characters in a sequence. These characters can be letters, numbers, punctuation symbols (except the period (.), which means concatenate) and spaces.
To store the string as the value of a variable, simply define the variable as that string, using quote marks (single or double):
The backslash character may be used to inform the programme that next character is to be used as a string character, and not an operator. For example, if a string is within single quotes, but it is desired to insert a string with a single quote as an apostrophe, it is necessary to 'escape' the quote with a backslash:
This will result in the string
'My friend' being stored in the variable $newVariable
To prevent this truncation, the apostrophe is escaped:
Variables may also be numbers, permitting calculations to be made dynamically and inserted into a page text or form.
Constants are named elements which do not change value in a particular script.
They are defined as follows:
String Operations (PHP) can carry out a number of useful operations on strings:
Content © Renewable.Media. All rights reserved. Created : August 10, 2014 Last updated :February 14, 2016
The most recent article is:
View this item in the topic:
and many more articles in the subject:
Mathematics is the most important tool of science. The quest to understand the world and the universe using mathematics is as old as civilisation, and has led to the science and technology of today. Learn about the techniques and history of mathematics on ScienceLibrary.info.
1791 - 1867
Michael Faraday, 1791 - 1867, was an English physicist whose work effectively created the field of electromagnetism.
Site © ScienceLibrary.info
Site Design and content © Andrew Bone
Injustice anywhere is a threat to justice everywhere.
Website © renewable.media | Designed by: Andrew Bone