Front-end learning of PHP's Discourse

Source: Internet
Author: User
Tags learn php
Front-end learning of PHP statements

Directory

[1] Conditional statement IF statement switch statement [2] loop statement while loop Do...while loop for loop foreach Loop

Conditional statements

For performing different actions based on different conditions

If statement

if code executed when (  condition) {true ;}

if (condition) {  trueElse  {  false  when executing code;}

if Code executed when (  condition) {true  ;} elseif (Condition  ) {trueelse  {   false code to execute at the time of execution;}

 
  php$t=date("H"); if ($t< "ten") {  echo "has a good morning!"  ElseIf ($t< ")  {echo " has a good day! "  Else  {  echo "has a good night!" ;}? >

Switch statement

Switch (expression) { case label1:  if expression = Label1;    Break ;    Case Label2:  if expression = label2;    Break ; default:  code  to being executedif  expression is different   from Both Label1 and Label2;}

 PhpSwitch($x){ Case1:Echo"Number 1";  Break; Case2:Echo"Number 2";  Break; Case3:Echo"Number 3";  Break;default:Echo"No number between 1 and 3";}?>

Looping statements

When writing code, you often need to run the same block of code repeatedly, and you can use loops to perform such tasks

While loop

 while {The condition is true) {  the code to execute;}

 Php$sum= 12;//the current level of hunger for small petsEcho"I'm hungry,:-(.";Echo"
"; while($sum<100){//the small pet's hunger level to 100, indicates that the small pet eats the full, does not have to continue to feed, does not have the satiety to continue to feed $num=Rand(1,20);//random numbers, simulating small-pet-fed buns $sum=$sum+$num;//Small pets eat small bread Echo"I haven't eaten enough yet!" "; Echo"
";}Echo"I'm finally fed up, ^_^".;?>

Do...while Cycle

The loop executes the code block first, then checks the condition and repeats the loop if the specified condition is true

 Do {  while (condition is true);

 
  PHP  $sum  = 0;     Do {    $numrand(1,6);   Gets the random number from 1 to 6, simulates the dice    $sum$sum  $num;   forward step  }while ($num==6);   Echo "The Do...while example executes and goes forward:". $sum . "
";? >

For loop

For loop statements, initialization is evaluated unconditionally before the start of the loop, and the loop condition is evaluated before each cycle begins. If the value is true, the loop is resumed, the loop body statement is executed, and if the value is False, the loop is terminated. Increment statements are executed after each loop

 for (init counter; test counter; increment counter) {  code to be executed;}    

 
   for ($x$x$x+ +)  {echo "number is:$x
"?>

foreach Loop

The Foreach loop applies only to arrays, which iterate through each key/value pair in the array. For each iteration of the loop, the value of the current array element is assigned to the $value variable, and the array pointer moves one at a time until the last array element is reached. Generally there are two ways: Do not remove the label, remove the

[1] Only value, do not remove the mark

foreach ($arrayas$value) {code to be  executed;}    

 
  $colors Array ("Red", "green", "blue", "yellow"foreach ($colorsas$value) {   echo "$value
";}? >

[2] Simultaneously remove the mark and value

foreach ($arrayas$index$value) {  

 
  $colors Array (  "r" = "Red",  "g" = "green",  "b" = "Blue",  "y" = "yellow"   foreach ($colorsas$key$value) {  Echo  $key. ":". $value. "
";}? >

1 floor Munho learn php
is PHP the first programming language in contact with a landlord?
Re: Blue ideal for small matches
@ Munho Learn php, I learn the front-end, mainly js,php understand can
  • 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.