PHP Programming Basic Grammar Quick start manual _php tips

Source: Internet
Author: User
Tags arrays case statement define function html form php programming php script alphanumeric characters

After the PHP script is named. PHP, the code is placed in the parentheses below:

<?php ...
? >

Echo can print information, similar to printf.

<?php
echo "Hallo world";
? >

End Each statement with a semicolon;.

PHP supports three kinds of annotation methods:

<?php
//First

# Second/

*
This is
multiline comment/
?>

In PHP, the capitalization of functions, classes, and keywords is the same thing:

<! DOCTYPE html>
 
 

However, all variables are case sensitive and need to be case-sensitive.

<?php
$color = "Red";
$Color = "BLACK";
echo "My car is". $color. "<br>";
echo "My car is". $Color. "<br>";
? >

The above example also points to the definition of variables and the syntax of string concatenation.

Variable naming rules:

PHP Variable rules:

    • The variable begins with the $ symbol followed by the name of the variable
    • Variable names must begin with a letter or underscore
    • Variable names cannot begin with a number
    • Variable names can only contain alphanumeric characters and underscores (A-Z, 0-9, and _)
    • Variable names are case sensitive ($y and $Y are two different variables)
    • The variable is created the first time the value is assigned. You do not need to declare the type of the variable.

Variables can have three different scopes:

PHP has three different scope of variables:

    1. Local (partial)
    2. Global (overall)
    3. Static (quiescent)

A variable declared outside of a function owns the Global scope and can only be accessed outside of the function.
Variables declared within a function have a local scope and can only be accessed within a function.
The following example tests variables with local and global scope:

<?php
$x = 5;//global scope function

myTest () {
 $y = 10;//local scope
 echo "<p> test function internal variable:</p>";
 echo "Variable x is: $x";
 echo "<br>";
 echo "Variable y is: $y";
} 

MyTest ();

echo "<p> variable:</p> outside of test function";
echo "Variable x is: $x";
echo "<br>";
echo "Variable y is: $y";
? >

Run Result:

To test variables inside a function:

The variable x is:
Variable y is: 10
To test a variable other than a function:

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

You can actually access it, but you need help with the Global keyword:

The global keyword is used to access globally variables within a function.
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
?>

PHP stores all of the global variables in an array named $GLOBALS [index] at the same time. The subscript contains the variable name. This array is also accessible within 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
?>

Typically, all variables are deleted when the function completes execution. However, sometimes I need not delete a local variable. This needs to be static:

<?php

function MyTest () {
 static $x =0;
 echo $x;
 $x + +;
}

MyTest ();
MyTest ();
MyTest ();

? >

Both Echo and print are available in PHP, and the only difference is that the print return 1,echo has no return value.

The Strlen function can return the length of a string.

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

<?php
Echo strpos ("Hello world!", "World");
? >

Use the Define function to define constants:

<?php
define ("greeting", "Welcome to w3school.com.cn!");
echo greeting;
? >

The Define function also has a third parameter that specifies whether it is case sensitive.

PHP's if-else statements are very similar to other languages, for example:

<?php
$t =date ("H");

if ($t < "") {
 echo "Have a good morning!";
} elseif ($t < "") {
 echo "Have a good day!";
} else {
 echo "Have a good night!";
}
? >

Switch-case statement:

<?php
switch ($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, the for statement and other languages are no different, look at foreach:

<?php 
$colors = Array ("Red", "green", "blue", "yellow"); 

foreach ($colors as $value) {
 echo "$value <br>";
}
? >

The real power of PHP comes from its function, which has 1000 built-in functions.

User-defined function declarations begin with a "function" of the form:

function functionname () {
The executed code;
}
As an example:

<?php
function writemsg () {
 echo "Hello world!";
}

Writemsg (); Call function
?>

//contains parameters
<?php function
familyname ($fname, $year) {
 echo $fname Zhang. Born in $year <br> ";
}

Familyname ("Li", "1975");
Familyname ("Mr", "1978");
Familyname ("Tao", "1983");
? >

//default parameter
<?php
function setheight ($minheight =50) {
 echo "the" is: $minheight <br > ";
}

SetHeight ();
SetHeight (); The default value of
setheight (135) will be used.
SetHeight (a);
? >

//return value
<?php
function sum ($x, $y) {
 $z = $x + $y;
 return $z;
}

echo "5 + 10 =". SUM (5,10). "<br>";
echo "7 + 13 =". SUM (7,13). "<br>";
echo "2 + 4 =". SUM (2,4);
? >

In PHP, there are three types of arrays:

    1. Index array-an array with a numeric index
    2. Associative array-An array with the specified key
    3. Multidimensional array-An array that contains one or more arrays

Indexed arrays:

$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 "<br>";
>

Associative arrays:

$age =array ("Peter" => "," "Ben" => "Panax Notoginseng", "Joe" => "43");

Or

$age [' Peter ']= '];
$age [' Ben ']= ' for "Panax Notoginseng";
$age [' Joe ']=];

<?php
$age =array ("Bill" => "," "Steve" => "Notoginseng", "Peter" => "the");
echo "Peter is". $age [' Peter ']. "Years old."
>




//Traverse
<?php
$age =array ("Bill" => "," "Steve" => "Notoginseng", "Peter" => "the");

foreach ($age as $x => $x _value) {
 echo "key=". $x. ", value=." $x _value;
 echo "<br>";
>

Array sorting methods have the following:

    • Sort ()-sort the array in ascending order
    • Rsort ()-Sort in descending pairs
    • Asort ()-Sort the associative array in ascending order by value
    • Ksort ()-Sort the associative array in ascending order according to the key
    • Arsort ()-Sort the associative array in descending order by value
    • Krsort ()-Sort the associative array in descending order according to the key

It is more difficult to understand the sort of key-value pairs.

<?php
$age =array ("Bill" => "," "Steve" => "Notoginseng", "Peter" => "the");
Asort ($age);
? >

<?php
$age =array ("Bill" => "," "Steve" => "Notoginseng", "Peter" => "the");
Ksort ($age);
? >

Super global variables, which are predefined global variables, can be used everywhere, with special meaning:

    • $GLOBALS: Referencing all variables available in the global scope
    • $_server: Holds information about headers, paths, and script locations.
    • $_request: Used to collect data submitted by HTML forms.
    • $_post: Used to collect form data after submitting an HTML form that submits method= "post". is also commonly used to pass variables.
    • $_get:$_get can also be used to collect form data after submitting an HTML form (method= "get").
    • $_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.