As a beginner when you start learning php web development, here are some php coding syntax you must know!
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.
Here we see how differently we can assign value to php variable and different data types in php, php supports the following data types:
<?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.
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; ?>
<?php $total = 20.25; echo $total; ?>
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 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.
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.
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.
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