Php basic syntax tutorial

Source: Internet
Author: User
Tags string back
Output statement: echo (), print (). instance: echo #39; hello #39;, #39; world #39; note: #, and ** variable: the variable must start with $ and be case sensitive, the first character must be... Output statement:Echo (), print ().

Instance: echo 'hello', 'world ';

Note:#,//,/**/

Variable:It must start with $ and be case sensitive. The first character must be a letter or underscore.

Link string:Use a dot (.) to connect two strings.

Constant:Use uppercase letters to name constants. constants do not use dollar signs like variables.

Define ('name', 'value ');

PHP runtime utilizes several predefined constants, including PHP_VERSION and PHP_ OS (server operating system)

Create a new date constant:

define('TODAY','February 3,2005'); echo 'Today is'.TODAY;

Difference between single quotes and double quotes:

Characters in single quotes are processed literally, while values in double quotes are interpreted.

Process HTML forms:

2. php

When you enter a data point submit, it will call 2.php:

 
 Thank you.$name.for the following comments:
$comments

n";?>

Manage Magic Quotes ):

Escape the single and double quotation marks in the value of the variable.

If 'is entered in comments in the preceding example,' is displayed '.

In PHP, there are two main types of Magic Quotes: magic_quotes_gpc, which applies to form, URL, and cookie data (gpc stands for get, post, cookie); magic_quotes_rentime, it is applicable to data retrieved from external files and database tutorials.

If Magic Quotes is enabled on the server, you can use the strips tutorial lashes () function to remove it.

$ Var = stripslashes ($ var );

This function will delete any backslash found in $ var. In the form example, this function converts the escape commit string back to its original unescaped value.

Condition statement:

If(isset($_REQUEST['gender'])) { $gender=$_REQUEST['gender'] } Else {   $gender=NULL; } If($gender=='M') {   ... }

Verify form data:

Isset () is used to test whether a variable has a value (including 0, FALSE, or an empty string, but cannot be NULL)

However, if the null string is TRUE, it is not an effective way to verify the text input and text box of the HTML form.

Empty () method:Check whether a variable has a NULL value (empty): NULL string, 0, NULL, or FALSE. The example is as follows:

if(!Empty(&_REQUEST['name']))    {   $name=stripslashes($_REQUEST['NAME'])    }   if($name)  {  ...

To check whether the submitted value is a number, use the is_numeric () function. you can use the strlen () function to check whether more than 0 characters are entered:

if(strlen($var)>0)  ...

Array:

An array can constitute a series of key-value pairs, each of which is a project or element of the array ).

PHP supports two types of arrays: indexed array and associative array. The former uses numbers as keys, and the latter uses strings as keys. The first index of the index array starts at 0 unless the specified key is displayed.

The array follows the same naming rules as any other variables.

Echo $ ar [2]; echo $ ar ['MD']; // -- the key of the Union index must be enclosed in single quotes.

When an array uses a string as its key, the output format is:

Echo "{$ array ['Il ']}";

When a number is used as its key, the output format is:

Echo "$ array [4]";

Ultra Global array:

$ _ GET, $ _ POST, $ _ SESSION, $ _ REQUEST, and $ _ COOKIE:

Php uses $ _ GET to store all the variables and values sent to the php script through the get method. $ _ POST stores all the data sent from HTML forms to php scripts using the post method, and $ _ COOKIE, all of which are subsets of $ _ REQUEST.

In the previous example, if we use post to retrieve all elements, we can directly use $ _ POST ['name'], for example:

$ Name = $ _ POST ['name'];

Create an array:

1. add an element to the array at a time:

$array[]="d"; $array['son']="BUJ";

2. use the array () function:

$ Arr = array ('a' => 'lh ', 'MD' => 'MH ');

You can use this function whether or not the key is explicitly set:

$ Arr = array ('DD', 'asd ');

If the first value with a numeric key is set, the value added later will have an incremental key:

$ Day = array (1 =>'s ', 'DD', 'SS') // $ day [2] = dd, $ day [3] = ss;

To create an array of consecutive numbers, you can use the range () function:

$ Ten = range (1, 10 );

Access array:

foreach($array as $value)  {...}

The foreach () loop iterates every element in $ array and assigns the value of each element to the $ value variable. to access the key and value, you can use:

foreach($array as $key=>value) { echo "The array value at $key is $value"; }

PHP instance:

  
   'January',    'February',    'March');$days = range(1, 12);$years = range(2000, 2011);echo '
  ';foreach ($months as $key => $value) {    echo "$valuen";}echo "";echo '
  ';foreach ($days as $key => $value) {    echo "$valuen";}echo "";echo '
  ';foreach ($years as $key => $value) {    echo "$valuen";}echo '';?>
 

To determine the number of elements in the array, you can use the count () or sizeof () function:

$ Var = count ($ array );

From PHP4.1, the range () function can also create arrays of consecutive characters:

$ Var = range ('A', 'z ');

You do not need to use the names $ key and $ value in the foreach loop. However, some are abbreviated as $ k and $ v. if you see the Invalid argument supplied for foreach () (an Invalid parameter is provided for foreach () error message, it means you are trying to use the foreach loop on a variable that is not an array.

Address:

Reprinted at will, but please attach the article address :-)

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.