PHP

PHP Syntax


PHP syntax can be written anywhere in document.

Just we have to start with “<?php” and end with ?>”.

PHP statement terminate with semicolon (;).

 

Syntax:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LearnCoding</title>
</head>
<body>
    <?php
        // Php starts here
    ?>
</body>
</html>

 

Echo statement:

The Echo statement is used to output text or HTML content to the browser. It can be used to display variables, strings, or a combination of both. The basic syntax for echo is:

 

Example :

<?php
	echo "Welcome to learnCoding";
?>

Note: There are multiple alternate keywords for Output.

 

PHP Case Sensitivity

Keywords (e.g., echo, whitespace, if, else, if else, while), classes, functions, user-defined functions, are not case-sensitive.

 

Example 1:

<?php
    // Php is not Case Sensitive
    EcHo "Output using EcHo"."<br>";  
    echo "Output using echo"."<br>";  
    ECHO "Output using ECHO"."<br>";
?>

However, Variables are Case Sensitive

 

Example 2:

<?php
    // But variables are Case Sensitive
    $skill = "Php";
    echo "My Skill is $Skill"."<br>"."<br>";
    echo "My skill is $skill"."<br>";
    echo "My SKILL is $SKILL"."<br>";
?>

 

Php Comments:

Comment is a piece of text within the source code that is not executed by the PHP interpreter.

PHP supports both single line and double line comments. 

 

Syntax: 

Single-Line Comments:

	Denoted by ‘//’

Anything after ‘//’ on a line is treated as a comment.

Multi-Line Comments:

	Enclosed between ‘/*’ and ‘*/’

It can span multiple lines.

 

 

Example:

<?php
    /* Comments
    Calculate the sum of two numbers*/
    $num1 = 5;
    $num2 = 10;
    $sum = $num1 + $num2;
    // Output the result
    echo "Sum of $num1 and $num2 is: $sum";
?>

 

Note: It is used to describe or explain any line of code, making it easier for other developers to understand the code.