Learning the coding structure, definitions and use of variables in PHP
5 minute(s) read
Published on: Dec 16, 2021
Updated on: Mar 23, 2022
Structure of a PHP script:
Before we talk about the rules of programming in the PHP language and its variables, it is better to pay attention to an interesting point. PHP is a language embedded in HTML. PHP code is embedded in HTML tags. This type of code is executed and converted to HTML code on the server-side. These HTML codes or even their output are sent to the client.
For example, for a program that prints a hello world message on the screen, the source code of the program is as follows:
Hypertext Preprocessor Codes for the print hello world
Assuming that this code is stored in a file called example1.php and in the test folder in the www folder of the wamp program, just run localhost/test/to run itexample1.php. Enter in the browser. After running it, the phrase hello world is displayed on the screen.
Now, if you right-click on the browser page and see the source code, you will see that there is no PHP code, and you only see HTML code. Note the following figure.
How to insert PHP codes:
To insert PHP codes, the following tag is used:
There is another method called short-open-tags. In this method, all you have to do is put the PHP codes in tags like this:
But to use this method, you must set the short-open-tag value to on in the php.ini file; otherwise, your code will not be executed.
Insert descriptions in the program:
According to software engineering standards, it is better for programmers to insert comments in the required places from the written code to increase the clarity of the program. This explanation will make it easier for the author and other programmers to update and understand the code in the future. There are two ways to insert comments.
To insert a one-line description, insert one of the # or // symbols at the beginning of the line.
To insert multi-line descriptions, we put the desired descriptions between the symbols /* and * / in the following form:
This language is Case sensitive:
PHP is Case sensitive, and in Hypertext Preprocessor, the two names Test and test are different. So especially when using variables, pay attention to this point so that your program does not get errors.
Specify the end of the commands:
In PHP language, to specify the end of the command, you must put a "; " symbol at the end of it. Otherwise, you will get an error message.
For example, executing the following code causes an error in the following figure:
Using the variables in PHP:
Like other programming languages, PHP language uses the variables to store data while temporarily running the code. Every variable has a name and a value.
To define variables in PHP, you must put a "$" symbol at the beginning of their names. To assign a value to a variable, the equal sign is used.
The rules for naming a variable:
Variable names can be a combination of letters, numbers, and underlines. The variable name can start with an uppercase, lowercase letter, or highlight symbol.
Types of variables in PHP:
There are different types of variables that hold different types of data. In PHP, specifying variables is optional. If the variable type is not defined, the system will consider the appropriate style for the data to be assigned to it.
In PHP, there are generally eight types of variables, which are:
Integer, decimal number, logic, Null, array, object and source variables.
In the following, these types are described with examples.
These types of variables can hold an integer without decimals. This integer can be positive or negative. In the following code, two variables are defined with the names "a" and "b," and then the 10 and 20 are put in them. Finally, the variables and their values are shown in the separate lines in the output.
The maximum value that a correct variable can hold is 2,147,483,647, and the lowest is its negative value.
In a Double variable, decimal numbers such as 0.25 can be stored. The following command defines the variable p with a value of 3.141592.
Logical variables are used to check whether a condition is true or false. These types of variables have one of the values true or false.
In the above code, two variables, "a" and "b," are defined, and the result of their comparison is a Boolean value put in a variable named result. These two variables have different values; therefore, the FALSE value is placed in the result variable, and due to this value, the message "a and b are not equal" is shown in the output.
A null value in PHP:
The empty weight is placed in that variable by assigning this value to a variable. If we check the variable with a Null value as a logical variable, the false value is returned.
These variables contain several characters. The value of the string variables must be enclosed in a single quotation mark or a double quotation mark.
When the value of a string variable is enclosed in a double quotation mark, and a variable is in the string, it prints its value instead of the variable.
But if the value of the string is enclosed in a single quotation mark and a variable name is in the queue, it will show the variable name in the output.
Note the example and its output in the image below.
There are many topics about variables such as arrays, objects, and source variables. These topics need more time, and we will explain them in the next.About our Android and iOS app development services