PHP programming basic syntax quick start manual _ PHP

Source: Internet
Author: User
This article mainly introduces some basic PHP programming syntax quick start knowledge, including PHP array and loop statements and other basic knowledge points. if you need a friend, refer to the name of the php script. php, the code is placed in the following brackets:

<?php.......?>

Echo can print information, similar to printf.

<?phpecho "hallo world";?>

Each statement is followed by a semicolon ;.

Php supports three annotation methods:

<? Php // first type # Second type/* This is a multi-line comment */?>

In php, the case sensitivity of functions, classes, and keywords is the same:

<?phpECHO "Hello World!
";echo "Hello World!
";EcHo "Hello World!
";?>

However, all variables are case sensitive and must be case sensitive.

<?php$color="red";$Color="black";echo "my car is " . $color . "
";echo "my car is " . $Color . "
";?>

The preceding example also illustrates the variable definition and the syntax of string concatenation.

Variable naming rules:

PHP variable rules:

  • The variable starts with the $ symbol, followed by the variable name
  • The variable name must start with a letter or underscore
  • The variable name cannot start with a number.
  • Variable names can only contain letters, numbers, and underscores (A-z, 0-9, and _)
  • Variable names are case sensitive ($ y and $ Y are two different variables)
  • The variable will be created when the value is assigned for the first time. You do not need to declare the type of the variable.

Variables have three different scopes:

PHP has three different variable scopes:

  1. Local)
  2. Global)
  3. Static)

Variables declared outside the function have a Global scope and can only be accessed outside the function.
Variables declared in the function have LOCAL scopes and can only be accessed within the function.
The following example tests variables with local and global scopes:

<? Php $ x = 5; // global scope function myTest () {$ y = 10; // echo in local scope"

Internal variables of the test function:

"; Echo" variable x is: $ x "; echo"
"; Echo" variable y is: $ y ";} myTest (); echo"

Variables other than the test function:

"; Echo" variable x is: $ x "; echo"
"; Echo" variable y: $ y ";?>

Running result:

Internal variables of the test function:

The variable x is:
The variable y is: 10
Variables other than the test function:

The variable x is: 5
The variable y is:
The strange thing is why global variables cannot be accessed in local functions.

Actually, it can be accessed, but it requires the help of the global keyword:

Global keywords are used to access global variables in functions.
To do this, use the global keyword before the variable (inside the function:

<? Php $ x = 5; $ y = 10; function myTest () {global $ x, $ y; $ y = $ x + $ y;} myTest (); echo $ y; // output 15?>

PHP also stores all global variables in an array named $ GLOBALS [index. The subscript contains the variable name. This array can also be accessed in the function and can be used to directly update global variables.

<? Php $ x = 5; $ y = 10; function myTest () {$ GLOBALS ['Y'] = $ GLOBALS ['x'] + $ GLOBALS ['Y'];} myTest (); echo $ y; // output 15?>

Generally, after the function is executed, all variables are deleted. However, sometimes I do not need to delete a local variable. Static is required to achieve this:

<?phpfunction myTest() { static $x=0; echo $x; $x++;}myTest();myTest();myTest();?>

Echo and print can be used in php. The only difference between the two is that print returns 1, and echo does not return a value.

The strlen function returns the length of a string.

The strpos function is used to determine the position of another string:

<?phpecho strpos("Hello world!","world");?>

Use the define function to define constants:

<?phpdefine("GREETING", "Welcome to W3School.com.cn!");echo GREETING;?>

The define function has a third parameter to specify whether it is case sensitive.

Php's if-else statement is similar to other languages. for example:

<?php$t=date("H");if ($t<"10") { echo "Have a good morning!";} elseif ($t<"20") { echo "Have a good day!";} else { echo "Have a good night!";}?>

Switch-case statement:

<?phpswitch ($x){case 1: echo "Number 1"; break;case 2: echo "Number 2"; break;case 3: echo "Number 3"; break;default: echo "No number between 1 and 3";}?>

While and for statements are no different from other languages. let's look at foreach:

<?php $colors = array("red","green","blue","yellow"); foreach ($colors as $value) { echo "$value 
";}?>

The real power of php comes from its functions. it has 1000 built-in functions.

The user-defined function declaration starts with "function:

Function functionName (){
Code executed;
}
For example:

<? Phpfunction writeMsg () {echo "Hello world! ";} WriteMsg (); // call a function?> // Include parameters <? Phpfunction familyName ($ fname, $ year) {echo "$ fname Zhang. Born in $ year
";}Familyname (" Li "," 1975 "); familyName (" Hong "," 1978 "); familyName (" Tao "," 1983 ");?> // Default parameter <? Phpfunction setHeight ($ minheight = 50) {echo "The height is: $ minheight
";} SetHeight (350); setHeight (); // The default value is 50 setHeight (135); setHeight (80);?> // Return value <? Phpfunction sum ($ x, $ y) {$ z = $ x + $ y; return $ z;} echo "5 + 10 =". sum (5, 10 )."
"; Echo" 7 + 13 = ". sum (7,13 )."
"; Echo" 2 + 4 = ". sum (2, 4);?>

In PHP, there are three types of arrays:

  1. Index array-array with numeric indexes
  2. Associated array-array with the specified key
  3. Multi-dimensional array-an array containing one or more arrays

Index array:

$ Cars = array ("Volvo", "BMW", "SAAB"); <? Php $ cars = array ("Volvo", "BMW", "SAAB"); echo "I like ". $ cars [0]. ",". $ cars [1]. "and ". $ cars [2]. ". ";?> // Count <? Php $ cars = array ("Volvo", "BMW", "SAAB"); echo count ($ cars);?> // Variable index array <? Php $ cars = array ("Volvo", "BMW", "SAAB"); $ arrlength = count ($ cars); for ($ x = 0; $ x <$ arrlength; $ x ++) {echo $ cars [$ x]; echo"
";}?>

Join array:

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

Or

$ Age ['Peter '] = "35"; $ age ['Ben'] = "37"; $ age ['job'] = "43"; <? Php $ age = array ("Bill" => "35", "Steve" => "37", "Peter" => "43"); echo "Peter is ". $ age ['Peter ']. "years old. ";?> // Traverse <? Php $ age = array ("Bill" => "35", "Steve" => "37", "Peter" => "43 "); foreach ($ age as $ x => $ x_value) {echo "Key = ". $ x. ", Value = ". $ x_value; echo"
";}?>

The array sorting methods are as follows:

  • Sort ()-sort arrays in ascending order
  • Rsort ()-sort arrays in descending order
  • Asort ()-sort joined arrays in ascending order by value
  • Ksort ()-sort joined arrays in ascending order by key
  • Arsort ()-sort Associated arrays in descending order by value
  • Krsort ()-sort Associated arrays in descending order by key

It is hard to understand that key-value pairs are sorted.

<?php$age=array("Bill"=>"35","Steve"=>"37","Peter"=>"43");asort($age);?><?php$age=array("Bill"=>"35","Steve"=>"37","Peter"=>"43");ksort($age);?>

Super global variables, that is, pre-defined global variables, can be used anywhere. they have special meanings:

  • $ GLOBALS: reference all available variables in the global scope
  • $ _ SERVER: stores information about the header, path, and script location.
  • $ _ REQUEST: used to collect data submitted by HTML forms.
  • $ _ POST: used to collect the form data after the HTML form submitted by method = "post. It is also used to pass variables.
  • $ _ GET: $ _ GET can also be used to collect form data after an HTML form (method = "get") is submitted.
  • $ _ FILES
  • $ _ ENV
  • $ _ COOKIE
  • $ _ SESSION

Other content can be found in form processing.

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.