There are a lot of ways to define variables in PHP. The most common way is defining variables abstractly using a dollar ($) sign. E.g.
$number = 1;
$a = ‘String’;
We can also use the define() function to define a variable in PHP. Like in the following example :
define($a, ‘name’); //The first parameter is the variable and the second parameter is the value.
All global variables are stored in arrays, which may or may not contain data. There are different types of arrays in PHP. Few of them are
We will learn about these arrays in future and how they can be used and what kind of data they can store. We can use these variables throughout the runtime of our PHP script. Once you have made yourself accustomed to how the variables in PHP work, you have a host of different function which operate on all the variables. I am going to list them and their uses.
1. empty($variable) – the following function returns TRUE if the variable is an empty string, or 0 (in case of an integer). It also returns FALSE or NULL.
2. is_null($variable) – checks if a variable is null.
3. isset($variables1, $variable2, …) – It accepts multiple variables as input but returns TRUE only if all variables are set.
4. Is_int($variable) - checks if a variable is of integer type. Similarly we have is_string() and is_numeric() functions.
5. print_r($variable) – Used to print contents of an array. We will discuss in future what are arrays in PHP.
A string in PHP is defines using variables and they work just as they would in any other programming language. There are some special functions for strings in PHP.
1. chunk_split($string,[length]) - Splits the string in chunks of size length.
2. Count_chars($string, mode) – Counts how many times all the characters have occurred in the
string. The result depends in the mode of counting.
3. chr($integer) – returns the correct character represented by its ASCII value.
4. explode(delimiter, $string) – Splits the string at the occurrences of the delimiter. E.g
For the following code snippet the O/p is shown -
$a = 'one';
//Example of one of the functions
$array = "2-345-567";
$a = explode('-', $array);
There are a lot of other functions and it is not possible to fit them here but you should go ahead and explore all the features. Once you get out there, you will find a function for almost every instant functionality that you might desire. Do not go through all the function at once. First read and understand the ones that you might use. Remember slow and steady!