Summary of PHP basic syntax examples and php basic syntax examples

Source: Internet
Author: User

Summary of PHP basic syntax examples and php basic syntax examples

This example describes the basic PHP syntax. We will share this with you for your reference. The details are as follows:

Demo1.php

<? Php // echo indicates the output to the browser. echo is actually a function. // double quotation marks enclose the string echo. ';/******/# Is this also a comment?> <? Echo "who asked Junyi. "?> <Script language = "php"> echo: Who asked Junyi. '; </Script>

Demo2.php

<? Php // deeply understand echo. echo is a function // echo function: outputs the string // echo return value to the browser: void has no return value echo, float; // print: outputs a string to the browser. // Print return value: integer echo '<br/>'; print ': Who asked Junyi. '; Echo' <br/> '; echo print': Who asked Junyi. '; // Printf: outputs a string to the browser. // Return value of printf: returns the string length echo '<br/>'; printf ("buy % d disc", 5); echo '<br/> '; echo printf ("buy % d disc", 5); // sprintf function: Keep the string in memory // sprintf return value: returns the echo of the string itself '<br/> '; echo sprintf ("buy % d disc", 5) ;?>

Demo3.php

<? Php // create a variable // What type, integer, String, float type, Boolean Type // when creating a variable, determine its type by assigning a value $ sum = 0; $ total = 1.22; $ sum = $ total; echo $ sum;?>

Demo4.php

<? Php // data type conversion // $ sum = 0; // $ total = 1.22; // $ sum = $ total; // implicit conversion // echo $ sum; $ sum = 0; $ total = $ sum; echo gettype ($ total); echo "<br/>"; $ total = (float) $ sum; // explicitly convert echo gettype ($ total);?>

Demo5.php

<? Php // settype () $ sum = 100; // convert the $ sum type to the string settype ($ sum, "string "); // at this time, 100 of $ sum is not a number, but a string "100" echo gettype ($ sum);?>

Demo6.php

<? Php // isset () and unset () // determine whether a variable exists // destroy a variable $ a = 5; // variable $ a already has unset ($ ); // If the variable $ a really exists, isset ($ a) returns a Boolean value of 1, empty echo isset ($ a); $ B = 0; // empty () is used to judge whether the value of a variable is null. If it is null, true is used; otherwise, false is used. // In other words, "", 0, "0", NULL, FALSE, array (), var (); // and objects without any attributes will be considered as empty echo empty ($ B);?>

Demo7.php

<? Php // type judgment function $ sum = 100; echo is_integer ($ sum);?>

Demo8.php

<? Php // $ sum is a floating point type $ sum = 22.22; // intval ($ sum) is converted into an integer // echo intval ($ sum); settype ($ sum, "integer"); echo $ sum; // What type is $ sum currently echo gettype ($ sum);?>

Demo9.php

<? Php // constant define ("TOTAL", 100); echo TOTAL; $ TOTAL = 200; echo $ TOTAL; echo $ _ SERVER ["SERVER_ADMIN"]; echo $ _ SERVER ["HTTP_HOST"]; echo phpinfo ();?>

Demo10.php

<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK"> <form method = "get" action = "Demo11.php"> Name: <input type = "text" name = "username"/> <input type = "submit" value = "submit"/> </form>

Demo11.php

<? Php // echo $ username; this short style cannot be used // It will confuse the relationship with common variables, // step 1, assign the received variable data to a variable // extract the value of the name of the previous form from value = "chaoyv" name = "username" // $ _ POST ['username'] The whole will return the "chaoyv" string // echo $ _ POST ['username']; // $ username = $ _ POST ['username']; // echo $ username; // $ username = $ HTTP_POST_VARS ['username']; // echo $ username; echo $ _ GET ['username'];?>

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.