Basic syntax

Variables in PHP start with a dollar sign $ followed immediately by a name.

animal = 'cat';

Strings (sequences of characters) are enclosed in quotation marks or apostrophes:

$a = "quotation marks";
$b = 'apostrophes';

Digits are not enclosed in quotation marks:

$a = 5;
$b = 10;
$c = 3.14159;

The variable name can only consist of English alphabet characters and numbers. The name always begins with a letter.

If the name consists of multiple words, it is customary to use the camelCase syntax (the first letter is lowercase and every next word begins with uppercase):

$cat = 'kitten';
$quickComputer = 'Of course mine!';
$busNumber = 1;

The name cannot contain spaces, dashes, hooks, commas, quotation marks, parentheses, and other special characters. The only special character allowed is an underscore .

Decimal numbers are written with a dot:

$pi = 3.14159;

Quite often it may be useful to perform mathematical operations directly when defining a variable:

$a = 5;
$b = 3;
$c = $a + $b;	// add 5 + 3
echo $c;		// prints 8

Insert the quotation mark or apostrophe correctly

Quotation marks and apostrophes must not be combined at will. If we decide to use a quotation mark, for example, I must also end the string with the quotation mark and not use it inside.

So this is wrong:

echo "<img src="image.gif">";

Because it is not clear where the string begins and ends. Quotation marks or apostrophes cannot therefore be plunged.

One possible solution is called escaping, when a backslash is written in front of the problematic character.

echo "<img src=\"image.gif\">";

Backslash says that the next character will be exactly the one we want to use.

For listing HTML code, however, it is preferable to wrap the entire string in apostrophes and then use quotation marks in the usual way:

echo '<img src="image.gif">';

Alternatively it can be rotated:

echo "<img src='image.gif'>";

How to get a variable from an url or from form

Addresses containing a question mark carry information about the input variables, so index.php? Page = contacts , for example, indicates a page variable with contacts value. The value of this variable is read as $ _GET ['page'] .

The question mark character is not related to the file name on the disk. It is always the same file to which we pass parameters in the address.

Definition of the variable content from the address

Some variables are available at the moment of running the script (and we can use them directly), this is called superglobal variables. For example, to read a value from a URL, use the variable $ _GET . The use is as follows:

$a = $_GET['a']; 

echo $a;

This script writes into the source code what is in the URL address after the question mark.

Attention, this demonstration is not safe! For example. If a dishonest visitor passes the HTML code to the URL, it will be inserted into the page and executed. Therefore, we must always treat the listing using the htmlspecialchars () function.

$a = $_GET['a']; 

echo htmlspecialchars($a);

What if we access the page without specifying? A = anything , $ _GET ['a'] will not exist and PHP will throw an error message. This condition must be treated with a condition and, in the absence of a variable, do nothing (or alternatively list alternative content). The existence of a variable can be verified by the isset () function.

if (isset($_GET['a'])) {
	$a = $_GET['a']; 

	echo htmlspecialchars($a);
} else {
	echo 'Variable "a" does not exist!';
}

Example with counting

We can do a lot of different things with URL variables. For example, add them and write the result:

echo $_GET['a'] + $_GET['b'];

If we want to write more input parameters into the URL, we have to separate them with the ampersand (&) character. For example, the address might look like this: index.php? A = 5 & b = 3 .

Linking Text Inputs (Strings)

We can also easily link 2 text entries (strings). This is done using the dot operator. You can link in a variable or on a dump.

$a = 'dog'; 
$b = 'cat'; 
 
echo $a . ' a ' . $b;

Prints dog and cat .

Previous chapter (First script) Next chapter (Types of cycles)

Back to Learn PHP main menu