Current Location: Home> Latest Articles> PHP Basics Tutorial: A Complete Guide from Variables to the phpinfo() Function

PHP Basics Tutorial: A Complete Guide from Variables to the phpinfo() Function

gitbox 2025-07-29

PHP Basics Guide

PHP is a widely used server-side scripting language that can be embedded in HTML to generate dynamic web pages. This article will provide a comprehensive overview of PHP's basic usage, helping beginners understand the core concepts of PHP programming.

Variables

In PHP, variables are declared using the $ symbol. The variable name must start with a letter or an underscore, and can be followed by letters, numbers, or underscores. PHP is a loosely-typed language, meaning the variable type will automatically change based on the value assigned to it.

Here’s an example of a variable:

$myVar = "Hello World!";
echo $myVar;

In the code above, $myVar is a string variable, and we use echo to output its value.

Operators

PHP supports various operators, including arithmetic, comparison, and logical operators.

Here are some examples of common operators:

$a = 10;
$b = 5;
// Arithmetic operators
echo $a + $b;
echo $a - $b;
echo $a * $b;
echo $a / $b;
echo $a % $b;
// Comparison operators
echo $a == $b;
echo $a != $b;
echo $a > $b;
echo $a < $b;
echo $a >= $b;
echo $a <= $b;
// Logical operators
echo $a && $b;
echo $a || $b;
echo !$a;

As shown above, PHP supports many common operators that allow basic arithmetic and logical comparisons.

Control Structures

PHP provides a variety of control structures, such as if statements, switch statements, for loops, while loops, etc. These structures are used in a similar way to other programming languages.

Functions

In PHP, we can define functions to organize and reuse code. Functions are defined using the function keyword, can take any number of parameters, and return a value.

Here’s an example of defining a function:

function add($a, $b) {
    return $a + $b;
}
echo add(2, 3);

The code above defines an add() function that takes two parameters and returns their sum.

phpinfo(); Demonstration

The phpinfo() function is a built-in PHP function that outputs the PHP configuration details, including the PHP version, installed extensions, server environment, etc.

phpinfo();

Running the code above will output an HTML table containing PHP configuration information. The phpinfo() function is very useful for debugging PHP configuration issues.

However, it is important to use phpinfo() with caution in production environments, as it may expose sensitive server information.