


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

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
