Usage of variables in PHP learning Notes

Source: Internet
Author: User
Tags php compiler
Variables are used to store values, such as numbers, strings, or function results, so that we can use them multiple times in the script to store values, such as numbers, text strings, or arrays once set

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

Once a variable is set, we can re-use it in the script. all variables in PHP start with the $ symbol.

PHP code insertion is intuitive ," "End.

All variables start with "$", for example, "$ money ". (definition operators are easy to imagine.) when you need to use them, you can define them. you can omit the definition of the variable type. The php compiler will automatically choose for us.

For condition judgment statements, php and c are similar. If (condition A) {process A}, there are else and elseif. Else is used to run A process other than condition A. elseif (condition N...) is the code that runs when other conditions are true.

The code is as follows:

  1. $ Sex = $ _ POST ["sex"];
  2. If ($ sex = "male ")
  3. {
  4. Echo "male ";
  5. }
  6. Elseif ($ sex = "female ")
  7. {
  8. Echo "female ";
  9. }
  10. Else
  11. {
  12. Echo "unknown gender ";
  13. }
  14. ?>

The code means to submit gender options in form. Final Judgment: male outputs male, female outputs female, and unknown output in other cases.

If. php can be tested here, and then switch. this is also easy to understand. similar to the combination of if and elseif, the switch-back code becomes concise and intuitive when many conditions need to be judged.

The code is as follows:

  1. Switch (variable name)
  2. {
  3. Case value 1:
  4. Code to be executed if e-xpression = label1;
  5. Break;
  6. Case value 2:
  7. Code to be executed if e-xpression = label2;
  8. Break;
  9. Default:
  10. Code to be executed
  11. If e-xpression is different
  12. From both label1 and label2;
  13. }

When the values in the variable meet "value 1" or "value 2", execute the code under two cases respectively, and then remember that each "case" must be separated by "break, when the value in the variable is neither "value 1" nor "value 2", execute the code below "default.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.