Things to learn in php coding syntax

As a beginner when you start learning php web development, here are some php coding syntax you must know!

  • How to write php code block.
  • What are the different data types in php.
  • Php operators how to handle string and math calculations.
  • Handling conditional logics like if-else, switch case etc.
  • How to work with php array, loops etc.
Let’s write php code!

Before we start learning php coding, let’s get familiar with some very basic php syntax and php data types that often we deal with.

Php code syntax block always starts with "<?php" and end with "?>"
So, if you want to write something on screen, you have to write like example below.

<?php
echo "I am learning PHP at WebTrainingRoom!";                
?>

Notice, "echo" is used when you want something to be written / printed on screen.

php data types and variables

Here we see how differently we can assign value to php variable and different data types in php, php supports the following data types:

  • String : String value must be within quotes.
    <?php
    $str = "Hello world!";
    echo $str;
    
    $name = "Atanu!";
    echo $name;
    ?>

    You can see how to declare a string variable and assign value, here "$str" is the variable , and "Hello World!" is value assigned to the variable. Finally "echo $str;" is for printing the value on screen.

  • Integer:

    Simply assign integer value to variable, and throughout the scope of the program, that particular variable will act like integer, we can add, multiply, subtract value from that variable

    <?php
    $total = 100;
    echo $total;
    
    //to multiply number
    $total = 100 * 10;
    
    //to add number
    $total = 100 + 10;
    ?>
    
  • Float / Double, just like above example we can assign Float value to a variable in php code.
    <?php
    $total = 20.25;
    echo $total;
    
    ?>
    
  • Boolean

    Boolean means, the variable can hold two values, either true or false; this type of variable is used for decision-making logic.

    <?php
    $v1 = true;
    $v2 = false;
    
    if($v1)
    {
    
    }
    
        ?>
    
  • Array

    Array is a collection type variable, we can store multiple values in array, like other programming language we also can define array in php with following syntax.

    <?php
    $colors=array("Green","Yellow","Blue","Red");
    echo "I like " . $colors[0] . ", " . $colors[2] . " and " . $colors[3] . ".";
    ?> 
    

    Array always starts from 0 index, the first element is 0.

  • Object

    Like all other object oriented programming language, in php, we can create custom class, then create a new instance of that class, often termed as object, you may learn php class object and php oops concepts to understand object better.

  • NULL
  • Resource
  • Date

    Date is one of the most important data type in any programming language, every language has some unique way to display date time property.

    date("Y-m-d")
    date("m")
    date("d")
    date("Y") 
    

    Learn more about date time function in Php.

How to include a file in PHP?

You may often required this, specially for creating template, just think how you display some common feature in all across the website.

include("filename.php") does same for you.

<?php include("left_navigation.php"); ?>

You may be interested to read following posts:

After learning this you should be able to start as Trainee PHP Developer

 
Php basic for beginners
Learn php programming, php web development with free tutorials
Other Popular Tutorials
PHP Examples | Join PHP Online Course