Home  >  Article  >  Backend Development  >  PHP study notes: basic syntax and variable definition

PHP study notes: basic syntax and variable definition

王林
王林Original
2023-10-09 08:03:381384browse

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

  1. Marks of PHP code

In PHP code, we often use "" to mark the beginning and end of PHP code, as follows:

// PHP code
?>

  1. Comments

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
*/

  1. Output

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.

  1. Variables

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;
?>

  1. Data type

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.

  1. String type variable

$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.

  1. Integer type variable

$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.

  1. Variables of array type

$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.

  1. Boolean type variable

$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn