Loading...

TapGen

Free online tutorials

img

PHP is a powerful server-side scripting language. Executed on the local server in web development. 

That means, to run a PHP script you will need an Apache server like Wamp, Xammp, and more.

This article will guide you in understanding and provide an overview of PHP syntax.

PHP code is stored as a text file with the extension ".php." A ".php" file contains HTML, CSS, and JavaScript code along with the PHP code.

What is PHP Syntax

PHP syntax is the rules defining, how PHP code is written and structured.

Here are the basic elements:

Php tag

PHP code is executed between PHP opening and closing tags <?php ... ?>.The most common PHP tag. You can also use <? ... ?> for short tags. 

  
<?php
  echo "Hello, world!";
?>

Explanation:

  1. Opening Tag: The script begins with <?php
  2. Echo Function: The echo function outputs the string "Hello World!" to the web page.
  3. Closing PHP Tag: Although optional in this case, you can end with ?>.

In PHP, statements must end with a semicolon (;). This indicates the end of a command.

.php file with HTML code and PHP code:

 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome Page</title>
</head>
<body>
    <h1>Welcome to My Website!</h1>
    
    <?php
    // PHP code to set
     echo "Welcome, TapGen!";
    echo "Hello, World!";
   ?>
    
</body>
</html> 

Using semicolons correctly is essential for the code to run without syntax errors.

Running the Code

To run this code:

  1. Save it in a file with a .php extension, e.g., example.php.
  2. Place the file in the root directory of a local server (like XAMPP or MAMP) or on a web server.
  3. Access the file through a web browser by navigating to http://localhost/example.php.

PHP Variables

Variables are used for storing information. Variables in PHP start with the $ sign.

Let's take a look at an example of PHP Variables:

  
<?php
$name = "John H";
$age = 25;
echo "My name is $name and I am $age years old.";
echo $name;
?> 
 

We will talk in detail about this in another article. 

Using if, else, and loops

The if statement allows you to execute code based on a condition. 

 
 <?php
$score = 85;

if ($score >= 90) {
    echo "Grade: A";
} elseif ($score >= 80) {
    echo "Grade: B";
} else {
    echo "Grade: C";
}
 
?>
 
 

Output

Grade: B

PHP has some more important syntax like

  • Arrays
  • Functions
  • Classes and Objects
  • Form Handling
  • Include Files
  • Comments
  • and more

We will discuss step by step every Syntax.

Feel free to modify the name and age to see how the output changes!

If you have any other questions or need further examples, let us know and upload the question in our forum tapgen.xyz

Learn to code with the world's largest web developer site. Not sure where to begin? HTML is The language for building web pages Learn HTML Tutorial with Tapgen.

=================================
Share This Article
icon

New to Communities?

Join the community