Php basic syntax tutorial

Source: Internet
Author: User
Tags arrays html form php script php tutorial string back strlen

Php Tutorial basics

Output statement:

Echo (), print ().

Eg:

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:

All 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, 200 ');

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

<Form action = "" method = "">

Indicates to submit the page to the specified page.

The PHP page for receiving form data will assign the content entered in this form to a special variable named & _ REQUEST ['name. All variables must be spelled in uppercase, because PHP is case sensitive to variable names. Name indicates the variable name specified in the form to be submitted.

 

1. php

<Body>

<Form action = "2.php" method =" get ">

<Fieldset> <legend> Enter your information in the form below: </legend>

<P> <B> name: </B> <input type = "text" name = "name" size = "20" maxlength = "40"/> </p>

<P> <B> E-mail: </B> <input type = "text" name = "email" size = "40" maxlength = "60"/> </p>

<P> <B> Gender: </B> <input type = "radio" name = "gender" value = "M"/> Male <input type = "radio" name = "gender" value = "F "/> Female </p>

<P> <B> Age: </B>

<Select name = "age">

<Option value = "0-29"> Under 30 </option>

<Option value = "30-60"> Between 30 and 60 </option>

<Option value = "60 +" Over 60> </option>

</Select> </p>

 

<P> <B> Comments: </B> <textarea name = "comments" rows = "3" cols = "40"> </textarea> </p>

</Fieldset>

 

<Div align = "center"> <input type = "submit" name = "submit" value = "Submit My Information"/> </div>

</Form>

</Body>

Run the following command:

 

2. php

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

<Body>

<? Php

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

$ Email = $ _ REQUEST ['email '];

$ Comments = $ _ REQUEST ['comments'];

 

Echo "<p> Thank you. <B> $ name </B>. for the following comments: <br/> <tt> $ comments </tt> </p> n ";

?>

</Body>

Run the following command:

 

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 this method is used, if the null string test is TRUE, it is not used to verify the text input of the HTML form.

The valid method of the input and text boxes.

Empty () method:

Check whether a variable has a NULL value (empty): NULL string, 0, NULL, or FALSE.

Eg:

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.

Arrays follow 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 data sent from HTML forms to php scripts using the post method.

And $ _ cookies are all subsets of $ _ REQUEST.

Eg:

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

       }

        

Eg:

       

<Form action = "2.php" method =" post ">

<? Php

$ Months = array (1 => 'January ', 'February', 'march ');

$ Days = range (1, 12 );

$ Years = range (2000,2011 );

Echo '<select name = "month"> ';

Foreach ($ months as $ key => $ value)

  {

Echo "<option value =" $ value "> $ value </option> n ";

  }

Echo "</select> ";

Echo '<select name = "days"> ';

Foreach ($ days as $ key => $ value)

  {

Echo "<option value =" $ value "> $ value </option> n ";

  }

Echo "</select> ";

Echo '<select name = "years"> ';

Foreach ($ years as $ key => $ value)

  {

Echo "<option value =" $ value "> $ value </option> n ";

  }

Echo '</select> ';

?>

 

 

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 an error message for Invalid argument supplied for foreach () (which provides Invalid parameters for foreach (), this means

You are trying to use the foreach loop on a variable that is not an array.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.