PHP syntax basics, PHP syntax _php tutorial

Source: Internet
Author: User
Tags html header

PHP syntax basics, PHP syntax


1.PHP Syntax Basics
    • $a = 10; Variables can be modified during the run process
$a = ten; $a =; $b = 5; Echo $a+$b;
    • Const THE_VALUE = 100; Constants can only be assigned one time, otherwise they will report a syntax error
Const The_value = +; // the_value = 200; error when running with syntax errors Echo The_value;
    • Methods for defining constants in PHP4
Define (' The_value ', $); // the_value = 200; syntax error Echo The_value;

2. Function wraps a lot of code blocks of functionality
    • Simple functions
function tracehellophp () {    echo ' Hello PHP ';     Echo '
'; Echo ' Hello world '; Tracehellophp ();
    • function as a callback function for other functions
function tracehellophp () {    echo ' Hello php--back ';     Echo '
'; Echo ' Hello world '; // tracehellophp (); $func = ' tracehellophp '; $func ();
    • Incoming parameters of a function
function SayHello ($name) {    echo ' Hello '.  $name;     Echo '
';} SayHello (' Zhangsan '); SayHello (' Lisi ');
    • Functions with multiple incoming parameters
function tracenum ($a,$b) {    echo ' a = '.  $a. ', B = '. $b. '
'; Echo $a $b
"; // It's also possible to write. }tracenum (2,3);
    • function with return value
function Add ($a,$b) {    return$a+$b ;} echo Add (10,2). '
';

3. Process Control
    • Garbled reason: The encoding of the server and the way the client encodes different solutions: Add the HTML header, and embed the PHP code in the body
    
 
      Control Flow
 
  PHP/**/function getlevel ($score) {    if( $score >=) {        return ' excellent ';    } ElseIf ($score >=) {        return ' good ';    } ElseIf ($score >=) {        return ' good ';    } ElseIf ($score >=) {        return ' can ';    } Else {        return ' bad ';    }} echo getlevel (92). '
';? >
    • Control Statement Notation 2
function getlevel_switch ($score) {    switch($score/10) {          Case         9:            return ' excellent ';          Case 8:            return ' good ';          Case 7:            return ' good ';          Case 6:            return ' can ';         default:            return ' bad ';    }} Echo Getlevel_switch (60). '
';
    • Control Statement Notation 3
function getlevel_break ($score) {    $result = '--';     Switch (intval($score/10)) {        case         9:            $ Result = ' excellent ';  Break ;          Case 8:            $result = ' good ';  break;          Case 7:            $result = ' good ';  break;          Case 6:            $result = ' may ';  break;    }     return $result ;} Echo Getlevel_break (83). '
';

4. Cycle
    • For loop
 for ($i= 0; $i<100; $i+ +)    {echo ' Hello '.  $i. '
';}
    • The while loop first judges and executes
$i = 0;  while ($i <) {    echo ' Hello '.  $i. '
'; $i+ +;}
    • Do-while Loop first performed in the judgment
$i = 0;  Do {    echo ' Hello '.  $i. '
'; $i+ +; // do not forget to increase, or you will be programmed to die loop } while ($i<20);
    • Break jumps out * the whole * loop
 for ($i= 0; $i<100; $i+ +)    {echo ' Hello '.  $i. '
'; if $i = = ) { break; }}
    • Continue Jump * this time * cycle
 for ($i= 0; $i<100; $i+ +)    {echo ' Hello '.  $i. '
'; // if ($i = =) {// break;// } if$i) { Continue; } Echo ' Run here '. $i. '
';}

5. Logical operation with && or | | | non-!
function Tracenum () {    for($i=0;  $i<=100; $i+ +) {//        if (0 = = $i%2 && 0== $i%3) {//            echo $i. '
';// }// if (0 = = $i%2 | | 0== $i%3) {// echo $i. '
';// } if$i%2) { echo$i. '
'; } }} Tracenum ();

http://www.bkjia.com/PHPjc/1069669.html www.bkjia.com true http://www.bkjia.com/PHPjc/1069669.html techarticle PHP syntax basis, PHP syntax 1.PHP syntax basis $a = 10; variables can be modified during operation $a = ten; $a = 100; $b = 5; echo $a + $b; Const The_value Can be assigned a value ...

  • 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.