Initial knowledge of PHP

Source: Internet
Author: User
Tags echo command learn php php print type null variable scope alphanumeric characters

the PHP script can be placed anywhere in the document. PHP script starts with <?php and ends with?> PHP files usually contain HTML tags and some php script code.<! DOCTYPE html><body> <?phpecho "Hello world!";?></body>Note: The PHP statement ends with a semicolon (;). The close tag of a PHP block also automatically indicates a semicolon (so you don't have to use a semicolon in the last line of the PHP block).Comments for PHP<! DOCTYPE html><body> <?php//This is a single-line comment# This is also a single-line comment/*This is a multiline comment block.it spans theMultiple Lines*/?></body>in PHP, all user-defined functions, classes, and keywords (such as if, else, Echo, and so on) are not case sensitive. in the following example, all three echo statements are legal (equivalent):<! DOCTYPE html><body><?phpECHO "Hello world!<br>";echo "Hello world!<br>";EcHo "Hello world!<br>";?></body>in PHP, however, all variables are case-sensitive. in the following example, only the first statement shows the value of the $color variable (because $color, $COLOR, and $coLOR are treated as three different variables):<! DOCTYPE html><body> <?php$color = "Red";echo "My car is". $color. "<br>";echo "My House is". $COLOR. "<br>";echo "My boat is". $coLOR. "<br>";?></body>Summary: The syntax is not sensitive in PHP, the variable name is case sensitive.Use of PHP variables: <?php$x =5;$y =6;$z = $x + $y;echo $z;?>A variable is a container for storing data. PHP Variable rules:
    • The variable starts with the $ sign, followed by the name of the variable
    • Variable names must begin with a letter or underscore
    • Variable names cannot start with a number
    • Variable names can contain only alphanumeric characters and underscores (A-Z, 0-9, and _)
    • Variable names are case sensitive ($y and $Y are two different variables)
Note:The PHP variable name is case-sensitive! The creation of a variable is created the first time it is assigned a value; Note: Enclose the value in quotation marks if the value you assign to the variable is text. The data type of the PHP variable is not to be told when assigning a value. PHP automatically converts the variable to the correct data type based on its value. PHP variable ScopeIn PHP, variables can be declared anywhere in the script. The scope of a variable refers to the part of the script that a variable can be referenced/used. PHP has three different scope of variables:
    • Local (partial)
    • Global (globally)
    • Static (statically)
variables declared outside the function have Global scope and can only be accessed outside of the function. variables declared inside a function have a local scope and can only be accessed inside the function. Unlike Python, the scope of PHP is that the global variable can only be referenced outside the function and cannot be found inside the function. Internal variables can only be referenced inside a function and cannot be referenced outside, and this is the same as Python. If you need a variable outside the function to reference the function, you need to use the Global keyword:<?php$x =5;$y =10; function MyTest () {global $x, $y;$y = $x + $y;}myTest ();echo $y;//Output?>Another way to do this:<?php$x =5;$y =10; function MyTest () {$GLOBALS [' y ']= $GLOBALS [' x ']+ $GLOBALS [' y '];}myTest ();echo $y;//Output?>PHP Static KeywordsTypically, all variables are deleted when the function finishes/executes. However, sometimes I need to not delete a local variable. Achieving this requires a bit of further work. to do this, use the static keyword when you first declare a variable:<?php function MyTest () {static $x =0;echo $x;$x + +;}myTest ();myTest ();myTest ();?>then, whenever the function is called, the information stored by the variable is the information contained in the last Call of the function. Note: The variable is still a local variable of the function. PHP Echo and Print statementsthe difference between Echo and print:
    • echo-capable of outputting more than one string
    • Print-only one string is output and always returns 1
tip:echo is slightly faster than print because it does not return any values. PHP Echo StatementEcho is a language structure that can be used with or without parentheses: Echo or Echo (). Display stringThe following example shows how to display different strings with the echo command (also note that the string can contain HTML tags):<?phpecho "echo "Hello world!<br>";echo "I ' m about to learn php!<br>";echo "This", "string", "is", "made", "with multiple parameters.";?>Show variablesThe following example shows how to use the echo command to display strings and variables:<?php$txt 1= "Learn PHP";$txt 2= "w3school.com. cn"; $cars =array ("Volvo", "BMW", "SAAB");echo $txt 1;echo "<br>";echo "Study PHP at $txt 2";echo "My car is a {$cars [0]}";?>PHP Print StatementPrint is also a language structure and can be used with or without parentheses: print or print (). Display stringThe following example shows how to use the Print command to display different strings (also note that the string can contain HTML tags):<?phpprint "print "Hello world!<br>";print "I ' m about to learn php!";?>Show variablesThe following example shows how to use the Print command to display strings and variables:<?php$txt 1= "Learn PHP";$txt 2= "w3school.com. cn"; $cars =array ("Volvo", "BMW", "SAAB");print $txt 1;print "<br>";print "Study PHP at $txt 2";print "My car is a {$cars [0]}";?>data types for PHPPHP stringThe string is a sequence of characters, such as "Hello world!". The string can be any text within quotation marks. You can use single or double quotation marks:Instance<?php$x = "Hello world!";echo $x;echo "<br>";$x = ' Hello world! ';echo $x;?>PHP integerintegers are numbers that have no decimals. integer rule:
    • Integers must have at least one number (0-9)
    • Integers cannot contain commas or spaces
    • Integers cannot have decimal points
    • Integers can be both positive and negative
    • Integers can be specified in three formats: decimal, hexadecimal (prefix 0x), or octal (prefix 0)
in the example below, we will test for different numbers. PHP Var_dump () returns the data type and value of the variable:<?php$x = 5985;Var_dump ($x);echo "<br>";$x = 345;//Negative numberVar_dump ($x);echo "<br>";$x = 0x8c;//16 binary numberVar_dump ($x);echo "<br>";$x = 047;//Eight binary numberVar_dump ($x);?>PHP floating-point numberfloating-point numbers are digits that have decimal or exponential form. in the example below, we will test for different numbers. PHP Var_dump () returns the data type and value of the variable:<?php$x = 10.365;Var_dump ($x);echo "<br>";$x = 2.4e3;Var_dump ($x);echo "<br>";$x = 8E-5;Var_dump ($x);?>PHP arraysAn array stores multiple values in a variable. in the following example, we will test different arrays. PHP Var_dump () returns the data type and value of the variable:<?php$cars =array ("Volvo", "BMW", "SAAB");Var_dump ($cars);?>PHP Objectobjects are data types that store data and information about how to work with the data. in PHP, you must explicitly declare an object. First we must declare the class of the object. For this, we use the class keyword. A class is a structure that contains properties and methods. We then define the data type in the object class, and then use this data type in an instance of the class:<?phpclass Car{var $color;function Car ($color = "green") {$this->color = $color;}function What_color () {return $this->color;}}?>PHP NULL ValueA special NULL value indicates that the variable has no value. Null is the only possible value for the data type NULL. The null value indicates whether the variable is empty. Also used to differentiate between empty strings and null-valued databases. you can empty the variable by setting the value to NULL:<?php$x = "Hello world!";$x =null;Var_dump ($x);?>Transferred from: http://www.w3school.com.cn/php/

Initial knowledge of PHP

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.