Home > Article > Backend Development > Getting Started with PHP: Start Learning from Basic Syntax
PHP is a widely used server-side scripting language that can be used in conjunction with HTML to create a variety of dynamic websites. PHP language is easy to learn and use, rich in resources, and is one of the preferred languages for website developers. In the process of learning PHP, you must first master the basic grammar of the language. Here I will tell you the basic grammar of PHP, hoping to help readers learn PHP better.
1. Basic PHP syntax
PHP’s syntax is similar to the syntax of many other languages, such as Perl and C, making it easy to learn. In PHP, all statements end with a semicolon, making it clearer when writing code. The following are some common PHP syntax:
For example:
<?php echo "Hello World!"; print "Hello World!"; ?>
The results displayed by the above code in the browser are "Hello World!".
For example:
<?php # This is a comment // This is another comment ?>
For example:
<?php $name = "Tom"; $age = 25; $height = 170.5; ?>
In the above code, $name is a string variable, $age is an integer variable, and $height is a floating point variable.
For example:
<?php $num1 = 10; // 整型 $num2 = 3.14; // 浮点型 $truth = true; // 布尔型 $name = "John"; // 字符串 $arr = array("苹果", "香蕉", "橙子"); // 数组 $obj = new stdClass(); // 对象 $nothing = null; // NULL ?>
For example:
<?php $num1 = 10; $num2 = 3; $sum = $num1 + $num2; // 加法运算符,值为13 $diff = $num1 - $num2; // 减法运算符,值为7 $prod = $num1 * $num2; // 乘法运算符,值为30 $quo = $num1 / $num2; // 除法运算符,值为3.33... $mod = $num1 % $num2; // 取模运算符,值为1 $cond = ($num1 > $num2); // 比较运算符,值为true $logic = ($num1 > 0) && ($num2 < 5); // 逻辑运算符,值为false ?>
For example:
<?php $greeting = "Hello"; $name = "Tom"; $message = $greeting . $name; // $message的值是"HelloTom" ?>
For example:
<?php $num = 10; if ($num > 0){ echo "这个数是正数"; } elseif ($num == 0){ echo "这个数是零"; } else{ echo "这个数是负数"; } ?>
The above code outputs "This number is a positive number" when the value of the variable $num is a positive number, and outputs "This number is a positive number" when the value of the variable is zero. Zero", when the value of the variable is a negative number, it outputs "This number is a negative number".
For example:
<?php for ($i = 1; $i <= 10; $i++){ echo $i." "; } echo "<br>"; $j = 1; while ($j <= 10){ echo $j." "; $j++; } ?>
The above code uses for and while loops to output numbers from 1 to 10 respectively.
For example:
<?php function square($num){ return $num * $num; } $result = square(5); echo $result; // 输出25 ?>
The above code defines a function named "square", which accepts a parameter, returns the square value of the parameter, and then calls the function and outputs return value.
2. Conclusion
These basic PHP syntax must be mastered by beginners. Subsequent PHP learning is inseparable from an in-depth mastery and understanding of these basic knowledge, which plays a decisive role in writing better PHP code. In the process of learning PHP, don't rush for success, but have a solid foundation, be brave enough to try, keep practicing, gradually hone your skills in practice, and eventually become an excellent PHP programmer.
The above is the detailed content of Getting Started with PHP: Start Learning from Basic Syntax. For more information, please follow other related articles on the PHP Chinese website!