In this entire article, I shall not be providing complete examples, since I assume you know enough of PHP to add the basic tags and other lines required to make a complete program. I shall only be discussing the important part of the programs.
Datatypes
To declare a variable it is sufficient to choose a name, through which the variable will be identified, and then initialize it. It is not necessary, as it happens for other programming languages, to declare the type of the variable. PHP is able to deduce the type of a variable from the value with which it has been initialized. For instance, if we write
PHP will create a variable of integer type with the name ' $var1 ' and store the value of 7 in it.
If instead we write
PHP Code:
$var2 = "This is a variable";
PHP will create a variable of string type named ' $var2 ' and store the value "This is a variable" in it.
The type of a variable indicates what kind of data the variable will store. The concept of datatypes is particularly important, not only in PHP, but in all programming languages.
A few of the datatypes in PHP are Integers, Floating Point numbers, Strings. I shall explain all of these in the rest of the article.
Datatypes : Integers
Variables of Integer type represent positive and negative integers, or zero. The declaration and initialization of an integer variable has the following syntax
PHP Code:
$VarName = IntValue;
$VarName is the name we have chosen for the variable, while IntValue has to be replaced with a datatype compatible number (a positive, negative or zero number).
The various operations that you can perform with integers are shown in the table below.
![Name: php2.jpg
Views: 101
Size: 34.4 KB](https://magentoexpertforum.com/attachment.php?s=3f98eaed17f7d4011ab3f216c5db6c6c&attachmentid=18&d=1369370721)
To make comparisons between integer variables we can use the comparison operators as shown previously while explaining conditional statements. They are summed up for you in the below table.
![Name: php3.jpg
Views: 114
Size: 13.7 KB](https://magentoexpertforum.com/attachment.php?s=3f98eaed17f7d4011ab3f216c5db6c6c&attachmentid=19&d=1369370786)
Datatypes : floating point numbers
Variables of floating point datatype are used to store numbers with a decimal part. The declaration and initialization of a floating point value has the following syntax
PHP Code:
$VarName = FloatValue;
$VarName is the name we have chosen for the variable, while FloatValue has to be replaced with a datatype compatible number.
PHP Code:
$x = 3.13;
$y = 0.143;
In order to execute operations with floating point numbers we can use the classic math operators as shown in the table for integer datatypes.
Note : You can carry out all the arithmetic operations with floats that you can perform with integers except the modulo operation. Obviously the module operator (%) lacks, because the division between decimal numbers has no remainder.
You can make similar comparisons between floating point values as you did with integer values.
Datatypes: strings
The declaration of a string variable is same as that of any other variable. The only difference is that the value that you want to store in a string variable must be enclosed in inverted commas or quotes. I have already talked about the existing difference between inverted commas and quotes previously. Here are two string variable declarations. The first one uses inverted commas, while in the second one quotes are used.
PHP Code:
$str1 = "This is a string datatype variable";
$str2 = 'This is a string datatype variable, too';
Previously I had said that PHP interprets all that is enclosed between inverted commas and carries out the needed substitutions; whereas it leaves unchanged all that is enclosed between quotes.
Let's look at some examples that will help us to be familiar with the PHP works. Observe the following script and try to guess the output.
PHP Code:
$Civicc = 8;
$Address = "Via Tespi, $Civic";
echo 'My address is $Address';
The script's output is displayed below
Code:
My address is $Address
If you guessed the output right it means you understood this series properly till now. If you have still have some trouble understanding why PHP has printed the variable name instead of its value, the answer once again is : PHP does not interpret the content between quotes.
Here is another script. Try guessing the output.
PHP Code:
$Civic = 8;
$Address = "Via Tespi, $Civic";
echo "My address is $Address";
The output of this script is as follows
Code:
My address is Via Tespi, 8
By replacing the quotes with the inverted commas in the third line, the PHP interprets the text before printing it. The $Address variable is replaced with its actual value at that instant ( in our program it is equal to Via Tespi, 8).
Note the double substitution that takes place in that line - One for $Address and another for $Civic.
Escape Characters
At this point you may have a doubt. Suppose you want to display something like this in an HTML page using PHP
The value of $Address variable is Via Tespi, 8
As a beginner you may try writing the following 2 scripts if you wanted the above output. but you would be disappointed with the actual output of both these programs
Code for Program 1
PHP Code:
$Civic = 8;
$Address = "Via Tespi, $Civic";
echo "The value of $Address variable is $Address";
Output of Program 1
Code:
The value of Via Tespi, 8 variable is Via Tespi, 8
---
Code for Program 2
PHP Code:
$Civic = 8;
$Address = "Via Tespi, $Civic";
echo 'The value of $Address variable is $Address';
Output of Program 2
Code:
The value of $Address variable is $Address
On possible solution to the problem is shown below
PHP Code:
$Civic = 8;
$Address = "Via Tespi, $Civic";
echo 'The value of $Address variable';
echo " is $Address";
This above script would display the required output, but we are compromising by using two echo statements rather than one.
An easier and better way exists. It is possible by using escape characters ' \ ' (backslash) between inverted commas, that allow printing of some special characters.
PHP Code:
$Civic = 8;
$Address = "Via Tespi, $Civic";
echo "The value of \$Address variable is $Address";
The above program would display the following output
Code:
The value of $Address variable is Via Tespi, 8
Even though we have used inverted commas, addition of the escape character ' \ ' before the variable name, prevents PHP from interpreting that variable.
Similarly in case you want inverted commas as a part of your output you could use the following
PHP Code:
$Civic = 8;
$Address = "Via Tespi, $Civic";
echo "The value of \$Address variable is \"$Address\""
This program would display the following
Code:
The value of $Address variable is "Via Tespi, 8"
There are other escape characters as well, that are used to represent special characters. These are shown in the table below.
![Name: php4.jpg
Views: 154
Size: 34.7 KB](https://magentoexpertforum.com/attachment.php?s=3f98eaed17f7d4011ab3f216c5db6c6c&attachmentid=20&d=1369370981)
That's all in this article. In the next and final article in this series, I shall explain Arrays which is a very common programming structure used by many beginners as well.
View more threads in the same category:
- buy registered drivers license (legitcleandocs.com)IELTS, id
- Buy Passport,Driver License,Age & ID Card,(Whatsapp:.......: +1 (551) 239-2904) Visas
- Buy Passport,Driver License,Age & ID Card,(Whatsapp:.......: +1 (551) 239-2904) Visas
- Buy Passport,Driver License,Age & ID Card,Visas and Undetected Counterfeit Money. Ema
- buy real drivers license(https://legitcleandocs.com)buy Austria id card, passports
- BUY DRIVER'S LICENSE ONLINE https://worldpassporte.com/
- Buy Passport,Driver License,Age & ID Card,(Whatsapp:.......: +1 (551) 239-2904) Visas
- buy real passports online(https://legitcleandocs.com)drivers license, visa, id card
- BUY UNDETECTABLE GRADE A+ COUNTERFEIT BANKNOTE(legitcleandocs.com)
- KjØp ekte pass pÅ nett
Bookmarks