Home  >  Article  >  Backend Development  >  Detailed explanation of variable usage in PHP study notes_PHP tutorial

Detailed explanation of variable usage in PHP study notes_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:16:14788browse

Variables are used to store values, such as numbers, strings, or the results of functions, so that we can use them multiple times in our scripts. Variables are used to store values, such as numbers, text strings, or arrays.

Once a variable is set, we can use it repeatedly in the script.

All variables in PHP start with the $ symbol.

PHP code insertion is very intuitive, starting with "".


All variables start with "$", such as "$money". (The definition is easy for people to imagine) When you need to use it, just define it. You can omit the definition of the variable type, and the PHP compiler will automatically choose it for us.


For conditional judgment statements, PHP is similar to C. If (condition A) {process A} is enough, there are also else and elseif. else is used to run processes other than condition A, elseif (condition N....) is the code that is run when other conditions are true.

The following is the code I wrote:

if($sex=="male")
The code is as follows
 代码如下 复制代码

if.php
  
  

  
  
男性  

  
女性  

  
  
  
$sex = $_POST["sex"];
if($sex=="male")
{
echo "男性";
}
elseif($sex=="female")
{
echo "女性";
}
else
{
echo "未知性别";
}

?>  
  
  
  

Copy code

if.php


Male


Female


 代码如下 复制代码
switch (变量名)  
{  
case 值1:  
  code to be executed if e­xpression = label1;  
  break;    
case 值2:  
  code to be executed if e­xpression = label2;  
  break;  
default:  
  code to be executed  
  if e­xpression is different   
  from both label1 and label2;  
}  

$sex = $_POST["sex"];
{ <🎜> echo "male"; <🎜> } <🎜> elseif($sex=="female") <🎜> { <🎜> echo "female"; <🎜> } <🎜> else <🎜> { <🎜> echo "unknown gender"; <🎜> } <🎜> <🎜> ?> The meaning of the code is to submit the gender options via POST through the form, which are male and female, and then hand it to php to receive the data from the post and pass it to the sex variable. Finally, the judgment is made. If it is male, the male will be output. If female, the female will be output. In other cases, the output is unknown. After submission The if.php mentioned in the article can be tested here Then there is switch, which is also easy to understand. It is similar to the combination of if and elseif. However, when many conditions need to be judged, switch makes the code very concise and intuitive. PHP code
The code is as follows Copy code
switch (variable name) { case value 1: code to be executed if expression = label1; break; case value 2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; } When the value in the variable meets "value 1" or "value 2", execute the code under the two cases respectively, and then remember to separate each "case" with "break;". When the value in the variable is neither "value 1" nor "value 2", the code below "default" is executed.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628688.htmlTechArticleVariables are used to store values, such as numbers, strings, or the results of functions, so that we can add more Variables are used to store values, such as numbers, text strings, or numbers...
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