Php Syntax with basic examples

PHP syntax Description:

Php Syntax with examples- hello everyone in this Article what I’m going to be covering is the PHP syntax and if you have any experience in other languages you know that there are a set of rules that you must follow in order to even start coding right and PHP has the same set of rules just code in a different way so if syntax determines is a little fuzzy for you basically it’s just a set of rules that must be followed in order to write properly structured code so with that being said it doesn’t even matter that which machine you are using Windows or Apple machine or Apple text editor here  I am using windows to demonstrate how to write PHP code if we use the same syntax it doesn’t matter when we upload our files to the server because the server is going to read the PHP syntax and if it’s the same it’s going to execute that file just the same so for PHP like I said we have special delimiter that we must write in order to create PHP scripting block.

PHP was designed as a server-side programming language that is tightly integrated into HTML. This is in contrast to the goal of other programming languages, code, and Separate content. Of course, such a separation is also possible in PHP, by including code in an external PHP file. But more common the PHP code is inserted directly into the HTML file. The file receives the Extension .php, .php4 or .php5. Mainly used today is the Use of .php.


Php Syntax:

PHP Syntax just starts off with a less than sign than a question mark then PHP and then it ends with a question mark and a greater than sign

so anything that we want to write for PHP we just write inside PHP scripting block.

Php Syntax forms:

standard form:

this PHP scripting block form is called a standard form.

Shorthand form:

it starts off with a less-than, then a question mark and it ends with a question mark and a greater than sign. so for the shorthand the reason I don’t recommend it is because of the server that you’re uploading your script to doesn’t have the shorthand method enable then your PHP code isn’t going to execute so for safety reasons and for compatibility with all servers they have PHP enable you to want to use the standard form.



ASP form

This form corresponded to ASP (Active Server Pages), which – meanwhile outdated server-side programming technology from Microsoft. This variant does not exist in PHP 7, the variant throws one there PHP Syntax error. The functionality is still available in older versions of Disposal. To do this, you have to add the asp entry in the Set configuration file php.ini to on.

HTML script tags form:

The last form has always been out of use in practice since it means a lot of typing. That is why it was also abolished in PHP 7. What all types have in common is that they are PHP statement blocks. You can include as many PHP blocks as you want in an HTML page.


Php syntax within Html:

If you’re curious about how can this tie in with HTML. So it’s quite simple so let’s just start our HTML like we normally would

I assume you have some HTML knowledge. So I’m not going to go into all of this but in a way, for HTML we could easily write inside body tag what we want to print on the screen i.e. I print Hello Programming digest

I’m just going to go ahead and save this file into the demo folder which is in my xampp server folder htdocs since this file is going to help PHP code include therefore I need to save this file with a dot PHP extension so I’m going to save this file with basicSyntax.php name

php syntax

Now I’m going to load this up in my browser real quick so simply write localhost /foldername where the file is saved then file name with extension in my case the path is localhost/demo/basicSyntax.php

php syntax

it says Hello Programming digest just fine.


now write Hello Programming digest inside the PHP scripting block

Save again and reload the page and see what happened

php syntax

As you can see PHP syntax error because PHP is a different language right so it’s going to have a different set of rules if you want to display something to the browser a discipline so the server can actually send that data to the browser so for that you will use the echo quotes or double quotes and it will be ended with the semicolon as you can see in the below programming

php syntax

As you can see the error is gone.


Php scripting inside Html extension:

I stated earlier since our file here our source file has PHP code included we have to save it with a dot PHP extension but let’s say I save below code as by mistake as a dot HTML extension

reload the HTML extension file into the browser and see what happened

php syntax

as you see it only prints the Hello Programming digest inside HTML body tag it doesn’t even recognize the PHP code block. so that’s very important if you notice one of the things to check if something’s not working correctly make sure you’re saving there with the right extension.

so going back and save it back as the PHP version

Reload the file again see what happened

php syntax

As you see with  PHP extension prints HTML as well as PHP instructions at the same time.



Semicolons in Php Syntax

All characters within a PHP statement block that are not commented out together form the PHP code that the PHP interpreter executes. Every A line in PHP that contains a statement is terminated with a semicolon:

php syntax

When you miss the semicolon what will happen

php syntax

So Semicolon is very important in PHP scripting.


Comments in PHP:

A comment is text in the source code that the PHP interpreter does not is performed. In practice, comments are used to break down parts of the code to explain reasons or to provide other information. PHP used syntax for comments that you may already have from JavaScript or know other languages:

stands for a one-line comment. All signs after are commented out.

also stands for a one-line comment.

comments on a block between / * and * / that also spans several Lines may extend.

In practice, a variant with two asterisks is often used opening comment: / ** … * / This is basically normal PHP comments, which are specially marked for phpDoc

Comment your code in a meaningful and understandable way. They think simply to the poor colleague who has to keep working on it, or to yourself, as you have forgotten after years what this is about Script should act. Either way, you will be human with make good comments happy!

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: