Php concise syntax

Source: Internet
Author: User
Php concise syntax 1 code block php uses Or Mark
2. php variables start with $
3 String processing:
3.1 string connector :.
3.2 string length strlen (string)
3.3 returns FALSE if the position of the substring strpos (str1, str2) is not found.
4 array $ Arryname = array (items, items) or $ Arryname = array (conststring => items, conststring => items) or $ Arryname [index] = items or $ Arryname [constString] = itmes or $ Arryname [index] = array ....
5. The foreach statement is foreach (array as value), which is different from other languages.
6. function definition function functionName (parameter ){}
7. obtain the value of the request parameter $ _ GET ["formItmeName"] get.
$ _ POST ["formItmeName"] post value
$ _ COOKIE ["cookieName"] cookie value
$ _ REQUEST ["QuestName"] get post cookie, etc.
8 date creation date mktime (hour, minute, second, month, day, year, is_dst)
Example $ tomorrow = mktime (0, 0, date ("m"), date ("d") + 1, date ("Y "));
Format date (format [, timestamp])
Example echo date ("Y/m/d"); echo"
"; Echo date (" Y. m. d ");
9. the include () function obtains all the text in the specified file and copies the text to the file using the include function.
The require () function is the same as include (), but the difference is that it handles errors.
The include () function generates a warning (but the script continues to execute), and The require () function generates a fatal error (the execution will be stopped when an error occurs ).

10 fopen () function is used to open a file in PHP.
$ File = fopen ("welcome.txt", "r ");
The fgets () function is used to read objects row by row from objects.
The fgetc () function is used to read files from a file character by character.
Fclose ($ file); close the file
If (feof ($ file) echo "End of file ";
Fclose ($ file );

11 using the Global Array $ _ FILES of PHP, you can upload FILES from your computer to a remote server.

If ($ _ FILES ["file"] ["type"] = "image/gif ")
| ($ _ FILES ["file"] ["type"] = "image/jpeg ")
| ($ _ FILES ["file"] ["type"] = "image/pjpeg "))
& ($ _ FILES ["file"] ["size"] <20000 ))
{
If ($ _ FILES ["file"] ["error"]> 0)
{
Echo "Return Code:". $ _ FILES ["file"] ["error"]."
";
}
Else
{
Echo "Upload:". $ _ FILES ["file"] ["name"]."
";
Echo "Type:". $ _ FILES ["file"] ["type"]."
";
Echo "Size:". ($ _ FILES ["file"] ["size"]/1024). "Kb
";
Echo "Temp file:". $ _ FILES ["file"] ["tmp_name"]."
";

If (file_exists ("upload/". $ _ FILES ["file"] ["name"])
{
Echo $ _ FILES ["file"] ["name"]. "already exists .";
}
Else
{
Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"],
"Upload/". $ _ FILES ["file"] ["name"]);
Echo "Stored in:". "upload/". $ _ FILES ["file"] ["name"];
}
}
}
Else
{
Echo "Invalid file ";
}


12 setcookie () is used to set the cookie.
Note: The setcookie () function must be locatedBefore the tag.
Setcookie (name, value, expire, path, domain );
Setcookie ("user", "Alex Porter", time () + 3600 );
The $ _ COOKIE variable of PHP is used to retrieve the cookie value.
// Print a cookie
Echo $ _ COOKIE ["user"];
// A way to view all cookies
Print_r ($ _ COOKIE );
Use the isset () function to check whether the cookie has been set.
If (isset ($ _ COOKIE ["user"])
When deleting a cookie, you should change the expiration date to the past time point.
Setcookie ("user", "", time ()-3600 );

13 before storing user information in a PHP session, you must start the session.
Note: The session_start () function must be locatedBefore the tag:
The correct method for storing and retrieving session variables is to use the PHP $ _ SESSION variable:
Session_start ();
// Store session data
$ _ SESSION ['View'] = 1;
Delete some session data. you can use the unset () or session_destroy () function.
The unset () function is used to release the specified session variable: unset ($ _ SESSION ['View']);
Session_destroy () function completely ends the session, session_destroy ();
Note: session_destroy () will reset the session and you will lose all the stored session data.

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.