As you know, Php is back-end programming language. This tutorial on Php Syntax will give an overview, that is very helpful for beginners in computer programming as well as in Php.
Basic Php Syntax
Php scripts execute on server side and server returns the plain HTML to the browser, and at last browser display the normal language content that is readable for non-tech people.
Php Syntax example
<?php // Your Php code goes here ?>
In the above example of Php syntax as you can see the basic structure.
<?Phpis the starting tag,
- the line where I wrote
"// Your Php code goes here"is the place, where you are going to write your whole Php code.
?>is the closing tag of Php.
Between the starting and closing tag of Php, you have to write your Php code. Writing outside of these closing and starting tag, will return your Php code in plain text, and make your scripts visible to users instead of working. So, keep in mind.
Comments in Php
Comments are extremely helpful in programming language. The most and probably only use of comments in any programming language is to write short notes about a program between your scripts or to commenting any running program for debugging, etc purposes. Server don’t run them and they don’t display on user screen. They are just for reference.
There are two ways to create comments in Php.
Single line comment:
Obviously, for writing a note in a single line. For example:
<?php // This is a Php comment # This way you can also write single line comments. ?>
For commenting something which cover more than one line of code. For example:
<?php /* This is a multi-line comment in Php. */ ?>
How to use comments in Php
As I already mentioned, that comments used for writing short notes about program to remember even after you check the code after a year or so. Here’s an quick example for using comments in a proper way:
$name = "Deepak Jangra"; // This echo statement display the name of user; echo $name;
Php Case Sensitive
Like most of other scripting languages, Php is case sensitive and also non-case sensitive.
Like variable names in Php are case sensitive. For example:
<?php $Name = "Deepak"; /// AND $name = "Deepslog";</pre> ?>
In above example, you have seen I created two variables using the $Name and $name, but because Php variables are case sensitive so, both variables are different for servers.
Apart from variables, KEYWORDS (like; if, else, echo, etc), pre-defined functions, classes, user defined functions are not case sensitive. For example:
<?php echo "Deepak this side!"; // or Echo "Deepak this side!"; // or ECHO "Deepak this side"; ?>
In above example, you saw I wrote three echo statements by mixing upper and lower case letter. And this is valid, equal, and work fine in Php.
How to use Php in HTML
I hope already know the basic structure of HTML. Here’s how you can use Php scripts between your HTML web page. You just need to simply type the opening, closing tag, and between those tags you can write your Php code. Here’s an example of writing Php in HTML:
<!DOCTYPE html> <html> <head> <title>My Web Page</title> </head> <body> <h1>How to display Php code in HTML</h1> <?php //opening tag // Your code echo "This is how you can write Php scripts in HTML"; //closing tag ?> </body> </html> ?>
So, this is the basic syntax of Php. I hope you have learnt all above things and tried all examples in your system. If you find any issue than leave a comment below with your code. I would be happy resolve that.
And WAIT..!!! It’s time to test.
Here’s a quick exercises for you to revise all above things.
Exercise 1: Find the missing (…)?
<!DOCTYPE html> <html> <head></head> <body> <?php /* Author: Deepak Website: deepslog.com ... echo "Hello there! Have you find the missing?" ?> </body> </html>
Exercise 2: Correct the wrong?
<!DOCTYPE html> <html> <head></head> <body> <?php Echo "See it..!!" $myname = "Deepak"; echo $Myname; ?> </body> </html>
I hope you have find the solution for both problems. If not let me know in the comments with your code.