


Detailed explanation of php variables, php variables_PHP tutorial
Detailed explanation of php variables, php variables
Variables are "containers" used to store information.
Syntax for defining a variable:
$变量名 = 值;
Example of using variables:
<?php $x=5; $y=6; $z=$x+$y; echo $z; ?>
Run
As you can see from this example, the variable name starts with $, indicating that it is a variable. Variable names start with letters (a-z, A-Z) or underscores _, and can be followed by any letters or numbers and underscores, but not spaces.
PHP variable rules:
- Variables start with a $ symbol followed by the name of the variable
- Variable names must start with a letter or underscore character
- Variable names can only contain alphanumeric characters and underscores (A-z, 0-9, and _ )
- Variable names cannot contain spaces
- Variable names are case-sensitive ($y and $Y are two different variables)
Note: PHP statements and PHP variables are case-sensitive.
The following variable names are valid:
$var_char $varChar $_varChar $var_char5
Create (declare) PHP variables
PHP has no command to declare variables.
A variable is created the first time you assign a value to it:
<?php $txt="Hello world!"; $x=5; $y=10.5; ?>
In the execution of the above statement, the variable txt will save the value Hello world!, and the variable x will save the value 5 .
Note: When you assign a text value to a variable, please put quotation marks around the text value.
PHP is a weakly typed language
In the above example, we noticed that it is not necessary to declare the data type of the variable to PHP.
PHP will automatically convert the variable into the correct data type based on its value.
In a strongly typed programming language, we must declare (define) the type and name of the variable before using it.
PHP variable scope
The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has four different variable scopes:
- local
- global
- static
- parameter
Local and global scope
Variables defined outside all functions have global scope. In addition to functions, global variables can be accessed by any part of the script. To access a global variable in a function, you need to use the global keyword.
Variables declared inside a PHP function are local variables and can only be accessed inside the function:
<?php $x=5; // 全局变量 function myTest() { $y=10; // 局部变量 echo "<p>Test variables inside the function:<p>"; echo "Variable x is: $x"; echo "<br>"; echo "Variable y is: $y"; } myTest(); echo "<p>Test variables outside the function:<p>"; echo "Variable x is: $x"; echo "<br>"; echo "Variable y is: $y"; ?>
In the above example, the myTest() function defines $x and $y variables. The $x variable is declared outside the function, so it is a global variable, and the $y variable is declared inside the function, so it is a local variable.
When we call the myTest() function and output the values of two variables, the function will output the value of the local variable $y, but cannot output the value of $x, because the $x variable is defined outside the function and cannot be inside the function Use, if you want to access a global variable in a function, you need to use the global keyword.
Then we output the values of the two variables outside the myTest() function. The function will output the value of all local variables $x, but cannot output the value of $y, because the $y variable is defined in the function and is a local variable. .
Note: You can use the same variable name in different functions, because the variable names defined in these functions are local variables and only affect that function.
PHP global keyword
The global keyword is used to access global variables within a function.
To call a global variable defined outside the function within a function, we need to add the global keyword before the variable in the function:
<?php $x=5; $y=10; function myTest() { global $x,$y; $y=$x+$y; } myTest(); echo $y; // 输出 15 ?>
PHP stores all global variables in an array called $GLOBALS[index]. index saves the name of the variable. This array can be accessed inside the function or used directly to update global variables.
The above example can be written like this:
<?php $x=5; $y=10; function myTest() { $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y']; } myTest(); echo $y; ?>
Static scope
When a function completes, all of its variables are usually deleted. However, sometimes you want a local variable not to be deleted.
To do this, use the static keyword the first time you declare the variable:
<?php function myTest() { static $x=0; echo $x; $x++; } myTest(); myTest(); myTest(); ?>
Then, each time the function is called, the variable will retain the value from the previous time the function was called.
Note: The variable is still local to the function.
Parameter scope
Parameters are local variables whose values are passed to the function through the calling code.
Parameters are declared in the parameter list, as part of the function declaration:
<?php function myTest($x) { echo $x; } myTest(5); ?>
Original address: http://www.manongjc.com/php/php_variables.html
php related reading:
php str_pad() function completes the string to the specified length
Analysis and explanation of usage of php str_replace() string replacement function
php str_ireplace() function is case-insensitive to replace characters in a string
php str_getcsv() function parses the CSV string into an array
php sscanf() function usage and examples

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
