Magento Expert Forum - Improve your Magento experience

Results 1 to 2 of 2

PHP Tutorial for Beginners - Part 4

  1. #1
    Moderator shunavi's Avatar
    Join Date
    Mar 2013
    Posts
    124
    Thanks
    9
    Thanked 26 Times in 17 Posts

    Lightbulb PHP Tutorial for Beginners - Part 4

    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 Code:
    $var1 7
    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: 96
Size:  34.4 KB

    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: 109
Size:  13.7 KB

    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: 149
Size:  34.7 KB

    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:


  2. #2
    Junior Member
    Join Date
    Sep 2018
    Location
    United Kingdom
    Posts
    156
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    PHP Tutorials for Beginners - Learn how to make a dynamic and intuitive site utilizing the PHP 7 programming language in straightforward and simple advances.

Similar Threads

  1. Magento Tutorial for Beginners (Part 4)
    By shunavi in forum Webmaster & Administrator
    Replies: 4
    Last Post: 07-04-2021, 06:31 AM
  2. PHP Tutorial for Beginners - Part 5
    By shunavi in forum PHP programming
    Replies: 2
    Last Post: 09-06-2020, 06:00 AM
  3. PHP Tutorial for Beginners - Part 1
    By shunavi in forum PHP programming
    Replies: 4
    Last Post: 06-06-2020, 10:47 AM
  4. PHP Tutorial for Beginners - Part 3
    By shunavi in forum PHP programming
    Replies: 0
    Last Post: 24-05-2013, 04:39 AM
  5. PHP Tutorial for Beginners - Part 2
    By shunavi in forum PHP programming
    Replies: 0
    Last Post: 24-05-2013, 04:34 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •