Basic PHP syntax
The PHP script can be placed anywhere in the document.
The PHP script begins with <?php and ends with ?> :
<?php//here is the PHP code?>
The default file name extension for PHP files is ". php".
PHP files usually contain HTML tags and some php script code.
PHP Case Sensitive
In PHP, all user-defined functions, classes, and keywords (such as if, else, Echo, and so on) are not case sensitive.
In the example below, all of these three-day echo statements are legal (equivalent):
Instance
<! DOCTYPE html>
In PHP, however, all variables are case-sensitive.
In the following example, only the first statement shows the value of the $color variable (because $color, $COLOR, and $coLOR are treated as three different variables):
Instance
<! DOCTYPE html>
PHP variables
A variable is a container for storing information:
Instance
<?php$x=5; $y =6; $z = $x + $y; echo $z;? >
Running an instance
Similar algebra
X=5y=6z=x+y
In algebra we use letters (such as x) to hold values (such as 5).
From the above expression Z=x+y, we can calculate the value of Z is 11.
In PHP, these three letters are called variables .
Note: Consider variables as containers for storing data.
PHP Variable rules:
The variable starts with the $ sign, followed by the name of the variable
Variable names must begin with a letter or underscore
Variable names cannot start with a number
Variable names can contain only alphanumeric characters and underscores (A-Z, 0-9, and _)
Variable names are case sensitive ($y and $Y are two different variables)
PHP is a loosely-typed language
In the example above, please note that we do not have to tell the data type of the PHP variable.
PHP automatically converts the variable to the correct data type based on its value.
In languages such as C and C + + and Java, a programmer must declare its name and type before using a variable.
PHP variable Scope
In PHP, variables can be declared anywhere in the script.
The scope of a variable refers to the part of the script that a variable can be referenced/used.
PHP has three different scope of variables:
Local (partial)
Global (globally)
Static (statically)
Local and Global Scopes
Variables declared outside the function have Global scope and can only be accessed outside of the function.
Variables declared inside a function have a local scope and can only be accessed inside the function.
The following example tests for variables with local and global scopes:
Instance
<?php$x=5; Global scope function MyTest () { $y = 10;//local scope echo "<p> variable:</p> inside the test function"; echo "Variable x is: $x"; echo "<br>"; echo "Variable y is: $x";} MyTest (); echo "<p> variable:</p> outside the test function"; echo "variable x is: $x"; echo "<br>" echo "Variable y is: $x";? >
PHP Global Keywords
The global keyword is used to access variables within the function.
To do this, use the Global keyword before (inside the function) variable:
Instance
<?php$x=5; $y =10;function myTest () { global $x, $y; $y = $x + $y;} MyTest (); Echo $y; Output 15?>
PHP Echo and Print statements
The difference between Echo and print:
echo "I ' m about to learn php!<br>"; echo "This", "string", "is", "made", "with multiple parameters." ;
This article explains the basic PHP syntax, more about the content, please follow the PHP Chinese web.