Home > Article > Backend Development > PHP study notes: basic syntax and variable definition
PHP study notes: basic syntax and variable definition
In today's Internet era, PHP (Hypertext Preprocessor), as a widely used server scripting language, has been surpassed Favored by more and more developers. This article will introduce you to the basic syntax of PHP and the definition of variables, and provide specific code examples to help beginners better understand and master it.
1. Basic syntax of PHP
In PHP code, we often use "" to mark the beginning and end of PHP code, as follows:
// PHP code
?>
Comments are a way to explain code. PHP supports two comment methods: single-line comments and multi-line comments. For example:
// This is a single-line comment
/*
This is a multi-line comment
You can write multiple lines
*/
The most commonly used output function in PHP is echo, which is used to print data to the browser, for example:
echo "Hello, PHP!";
?>
This code will output "Hello, PHP!" on the browser. In addition to echo, you can also use the print function to implement the output function.
In PHP, variables are used to store data. You need to declare the variable and define its type before using it. The variable name starts with the $ symbol, followed by the variable name, for example:
$name = "John";
$age = 25;
?>
PHP supports multiple data types, including integer (int), floating point number (float), string (string), Boolean value (bool), and array (array), object (object), etc. Depending on your needs, different data types can be used to store and process data.
2. Examples of variable definitions
The following are some specific code examples to help us better understand and master the definition of variables.
$name = "John";
$age = 25;
//Output variable value
echo "My name is " . $name . ".";
echo "I am " . $age . " years old.";
?>
The above code will output the following results:
My name is John.
I am 25 years old.
$num1 = 10;
$num2 = 5;
//Output variable value
echo "The sum is " . ($num1 $num2) . ".";
?>
The above code will output the following results:
The sum is 15.
$fruits = array("apple", "banana", "orange");
// Output the elements in the array
echo "I like " . $fruits[0] . ", " . $fruits[1] . " and " . $fruits[2] . ".";
?>
The above code will output the following Result:
I like apple, banana and orange.
$isTrue = true;
//Output variable value
if ($isTrue) {
echo "The statement is true.";
} else {
echo "The statement is false.";
}
?>
The above code will output the following result:
The statement is true.
To sum up, this article introduces the basic syntax of PHP and the definition of variables, and provides specific code examples. . By learning and practicing these basic knowledge, we can better understand and master the PHP language and lay the foundation for further learning and development. I hope these study notes can be helpful to PHP beginners.
The above is the detailed content of PHP study notes: basic syntax and variable definition. For more information, please follow other related articles on the PHP Chinese website!