PHP Syntax

The syntax of PHP is similar to other programming languages, with a few key differences. Here are a few basic rules to keep in mind when writing PHP code:

  1. PHP code is enclosed in special start and end tags: The opening tag is <?php and the closing tag is ?>. Everything between these tags is considered PHP code.
  2. PHP statements must end with a semicolon: Each PHP statement must be terminated with a semicolon (;). This tells the PHP interpreter that the statement is complete and it can move on to the next one.
  3. PHP is case-sensitive: PHP is case-sensitive, which means that variables, functions, and keywords must be written in the correct case. For example, $myVariable and $myvariable are considered to be different variables.
  4. PHP ignores white space: PHP ignores white space (spaces, tabs, and new lines) in your code, so you can use these to structure and format your code as you like.
  5. PHP supports single-line and multi-line comments: Single-line comments are denoted by a # symbol or by //, and multi-line comments are denoted by /* and */ .

Here is a simple example of a PHP script that outputs the text “Hello, world!” to the web browser:

<?php

# This is a single-line comment

/*
This is a
multi-line comment
*/

echo "Hello, world!";

?>

PHP Case Sensitivity

In PHP, variables, functions, and keywords are case-sensitive, which means that $myVariable and $myvariable are considered to be different variables. However, PHP is not case-sensitive when it comes to the names of functions and classes, so you can use function myFunction() and function myfunction() to define the same function.

It is a good idea to be consistent with the case that you use for your variables, functions, and keywords. Many PHP developers use all lowercase letters for these elements, but you can choose any case convention that you like as long as you are consistent.

Here are a few examples to illustrate the case sensitivity of PHP:

<?php

$myVariable = "hello";
$myvariable = "world";

echo $myVariable;  # Outputs "hello"
echo $myvariable;  # Outputs "world"

function myFunction() {
  echo "This is my function.";
}

function myfunction() {
  echo "This is also my function.";
}

myFunction();  # Outputs "This is my function."
myfunction();  # Outputs "This is also my function."

?>