PHP basic learning syntax

Source: Internet
Author: User
1. PHP script block? Php starts? End .? Php? 2. You can use echo or print to output text to the browser. Note that every script in PHP should end with a semicolon. The usage is as follows :? Phpechohellobr; print (world );? 3. PHP comments and c and java classes can be used or **, as shown below :? Phpechohello

1. PHP script block? Php starts? End. ? Php? 2. You can use echo or print to output text to the browser. Note that every script in PHP should end with a semicolon. The usage is as follows :? Phpecho hellobr/; print (world );? 3. PHP comments and c and java classes can be // or/**/, as shown below :? Php // echo hello

1. the PHP script block uses Start?>End.

 

2. You can use echo or print to output text to the browser. Note that every script in PHP should end with a semicolon ";". The usage is as follows:

  ";print("world");?>

3. PHP comments are similar to c and java. You can use // or/**/, as shown below:

  ";/*print("world");*/?>

4. Variables in PHP start with $ and do not need to specify the variable type. The usage is as follows:

  ";$age = 20;echo $age;echo "
";echo $name . " is " . $age . " years old.";?>
The above script is output in the browser as follows:


Note: The point "." is used to connect strings in PHP "."

5. Evaluate the string length function strlen (), which returns the string length value as follows:

  
The output result of the above script in the browser is: 13

6. strpos ($ str1, $ str2) function. This function has two string-type parameters used to retrieve the start position of str2 in str1. The usage is as follows:

  

The output result of the above script in the browser is: 4. If str2 is not matched in str1, the browser will not output

7. The operators in PHP are similar to those in java and c. It should be noted that PHP can directly use "=" to compare whether two strings are equal, the test code is as follows:

  ";}else{echo "not equals
";}}$str1 = "hello";$str2 = "hello2";$str3 = "hello";equals($str1, $str2);equals($str1, $str3);?>

The above script defines a function named equals, which has two parameters. In the function body, if the two parameters are equal, equals is output; otherwise, not equals is output, the above code is output in the browser, as shown in:


Note that the function definition in PHP starts with function and adds the function name and the parameter name in brackets.

8. The Condition Statement in PHP is similar to the Condition Statement in java. However, the keyword elseif is added in PHP. The usage is as follows:

   $b){echo "a > b";}elseif($a < $b){echo "a < b";}else{echo "a = b";}?>
9. The switch statement in PHP is similar to that in java, and the switch in PHP supports strings. The usage is as follows:

  

10. Use of arrays in PHP:

There are two methods to create an array, as shown in the following script:

  ";}// Method 2 for creating an array $ colors [0] =" red "; $ colors [1] =" blue "; $ colors [2] = "green"; foreach ($ colors as $ color) {echo $ color."
";}?>

The above script is output in the browser:


Note that the content in the array is output cyclically using foreach.


The array in PHP has a usage, especially similar to the Map in java, that is, the array can be stored in the form of key-value pairs. See the following script:
   23, "jack" => 18, "herry" => 21);echo $names["jack"];?>
In the above script, we assigned an age to a person with different names, and then we can retrieve his or her age based on the name.

11. PHP functions have been used before. PHP functions can contain parameters and return values. The sample code is as follows:

  

12. PHP mainly uses PHP built-in variables $ _ GET and $ _ POST to process the data submitted by forms. We can see from the name that, $ _ GET is used to receive data submitted by form in GET mode, and $ _ POST is used to receive data submitted by form in POST mode. We create two new files: login. php and result. php, set login. the form data in php is submitted to result. php:

The login. php script is as follows:

The result. php script is as follows:
 

  

The access results in the browser are as follows:

If the method in the form is get, $ _ GET ["username"] must be used in result. php to obtain the input value.

The built-in variable $ _ REQUEST in PHP can obtain the form data submitted in get or post mode.

13. date () function of PHP formatting time

The date () function of PHP has two parameters. The first parameter is the format of formatting, which is a required parameter, and the second parameter is the timestamp, which is an optional parameter,

The first parameter of the date () function specifies how to format the date/time. It uses letters to indicate the date and time formats. Some available letters are listed here:

  • Day in d-month (01-31)
  • M-current month, in numbers (01-12)
  • Y-current year (four digits)
The following code is used to output the current year, month, and day:
  
You can use the mktime () function to return a timestamp. The mktime () function has many parameters, as shown below:

mktime(hour,minute,second,month,day,year)
The following code outputs the date of tomorrow:
  
Here we use date ("m") to get the current month, date ("d") to get the current day, date ("Y") to get the current year

14. Introduce other files in PHP

There are two ways to introduce other files in PHP: include or require. The difference between the two functions is that they handle errors differently. If include introduces a page that does not exist, the warning information will be displayed on the page, but the script after the include operation will continue to be executed. If you use require to introduce a page that does not exist, the page will display the warning information, and the script after the require operation will not be executed.

15. PHP operation files

The following script uses PHP to read and output the file content by row:

  ";}?>
PHP uses the fopen function to open a file. The first parameter specifies the file path, and the second parameter specifies the file opening method. The following methods are available:

Mode Description
R Read-only. Start at the beginning of the file.
R + Read/write. Start at the beginning of the file.
W Write-only. Open and clear the file content. If the file does not exist, create a new file.
W + Read/write. Open and clear the file content. If the file does not exist, create a new file.
A Append. Open and write to the end of the file. If the file does not exist, create a new file.
A + Read/append. Write content to the end of the file to keep the file content.
X Write-only. Create a new file. If the object already exists, FALSE is returned.
X +

Read/write. Create a new file. If the file already exists, FALSE and an error are returned.

NOTE: If fopen () cannot open the specified file, 0 (false) is returned ).


The feof () function is used to determine whether to read the end Of the file. The fgets () function is used to read a row of the file. After reading the row, the file pointer automatically jumps to the next row, the fgetc () function is required to read files character by character. If the file is closed, the fclose () function is used ()

Today's learning record is here.

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.