Java Certification Training Course
- 47k Enrolled Learners
- Live Class
In the previous PHP Tutorial we learnt the various ways to differentiate the PHP code from the HTML code, so that it becomes easy for the PHP parser to interpret the code. In this post, let’s learn how to store information in the PHP script. Storing information is necessary so that you can use the value anywhere in the program.Variable is a name given to store values that is to be used in the program. In PHP, the variable names are preceded with the ‘$’ symbol.
<?php $a=10; $b=10; $c=$a+$b; echo $c; ?>
There are certain rules that must be followed when naming the variables. They are:
Variables are defined with a preceded dollar sign ($)
PHP variables must start with a letter or underscore “_”
PHP variables should contain only alpha-numeric characters and underscores
A variable name cannot start with a number
We can separate the variable names by using underscores. E.g.$employee_details
Variable names differentiates both lower case letters and upper case letters($y and $Y are two different variables)
Values can be assigned using the “=” operator
Another important thing in PHP is that all the statements must end with a semicolon “;”
The first step to use a variable, is to assign a value to it.
Variable assignment is simple. Just write the name and add a single equal sign (=), and then expression that we want to assign to that variable.
Example: $pi=3 + 0.1489;
After assigning a value to the variable, if it has to be changed in the later stages of the program, you can also reassign the values to the same variable name.
$my_num_var = “this should be a number – hope it is reassigned later”; $my_num_var = 5;
Many programming languages reports an error if you try to use the variables before they are assigned any value. But the PHP deals with such unassigned variables. In PHP, the default error-reporting settings allow you to use the unassigned variables without reporting any errors. If you would like to be warned about variables that have not been assigned, the error-reporting level to E_ALL from the default level of error reporting.
This can be done in two ways:
By including the statement error_reporting(E_ALL) at the top of script.
By changing the php.ini file to set the default level.
When you do not pass values to the parameters in functions, PHP assigns a value by default, which is called the default value
Variables in PHP do not have intrinsic types
A variable does not know in advance whether it will be used to store a number or string of characters
The type of a variable is interpreted depending on the context in which it is used
The data to be stored and used in the PHP script can be of various types. In order to differentiate this, PHP provides various Data Types.
The values stored and used in the PHP script are of different types. The Data Types define the type of data being used in the program. PHP has 8 Data Types. They are:
Integers are whole numbers, without a decimal point, like 495
Doubles are floating point numbers, like 3.1415 or 49
Booleans have only two possible values: TRUE and FALSE
NULL is a special data type. It has only one value: NULL
Strings are series of characters
Arrays is a collection of values
Objects are instances of classes, which can access both data and functions of that specific class
Resources are special variables that hold references to resources external to PHP(db connection)
These data types are classified into 2 major types:
Simple Types – integers, doubles, Booleans, Null and strings
Compound Types – strings, arrays, objects, resources
Simple Data Types:
Integers are of the simplest type. They correspond to simple whole numbers, both positive and negative.
$int_var = 6789; $another_int = -1245+134 //will zero
Doubles and Floating Point Numbers:
Real numbers (i.e., numbers containing a decimal point)
$first_double =568.998; $second_double = 0.444; $even_double =6.0;
Booleans are true – or – false values, which are used in control constructs like the testing portion of an if statement
To use Booleans, PHP provides a couple of constants: TRUE and FALSE
If(TRUE) Print(); Else Print();
NULL type, however, takes this to the logical extreme: The type NULL has only one possible value, which is the value NULL
$my_var = NULL;
A variable that has assigned to null has the following properties:
It evaluates to FALSE in a Boolean context
It returns FALSE when tested with IsSet()
Apart from declaring numbers, PHP also supports “Strings” where sequence of characters are treated as a single unit.
Strings are a sequence of character that are treated as one unit.Strings in PHP are declared in two ways:
Single Quoted String:
Here the statement present within the single quotes will be displayed as it is without any changes.
<?php $string_variable = "name"; $literally = 'My $string_variable is Happy! '; print($literally); ?>
My $string_variable is Happy!
Double Quoted String:
Here the statement present within the double quotes will be interpreted and the output of the program will be displayed.
<?php $string_variable = "name"; $literally = “My $string_variable is Happy! ”; print($literally); ?> Output:
My name is Happy!
When we want to execute a block of statements repeatedly, functions are used.
Stay tuned for our next post on how to pass parameter to functions and the various built-in functions supported in PHP.
Got a question for us? Please mention them in the comments section and we will get back to you.