PHP pseudo-static four methods summary

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags array code echo get html name network network programming

PHP pseudo-static use is mainly to hide the passed parameter name. Today we introduce to you a total of four pseudo-static PHP method, I hope we can further deepen the understanding of PHP pseudo-static through these four methods.
Here's the full method code:
[pre]

<? php
// pseudo static method one
// localhost/php100/test.php?id|1@action|2 $ Php2Html_FileUrl = $ _SERVER ["REQUEST_URI"];
echo $ Php2Html_FileUrl. "<br>"; // /php100/test.php?id|1@action|2 $ Php2Html_UrlString = str_replace ("?", "", str_replace ("/", "", strrchr (strrchr ($ Php2Html_FileUrl, "/"), "?")));
echo $ Php2Html_UrlString. "<br>"; // id | 1 @ action | 2 $ Php2Html_UrlQueryStrList = explode ("@", $ Php2Html_UrlString);
print_r ($ Php2Html_UrlQueryStrList); // Array ([0] => id | 1 [1] => action | 2) echo "<br>";
foreach ($ Php2Html_UrlQueryStrList as $ Php2Html_UrlQueryStr) {
$ Php2Html_TmpArray = explode ("|", $ Php2Html_UrlQueryStr); print_r ($ Php2Html_TmpArray); // Array ([0] => id [1] => 1); Array ([0] => action [1] => 2 )
echo "<br>"; $ _GET [$ Php2Html_TmpArray [0]] = $ Php2Html_TmpArray [1];
} // echo 'False static: $ _ GET variable <br />';
print_r ($ _ GET); // Array ([id | 1 @ action | 2] => [id] => 1 [action] => 2) echo "
echo "<hr>"; echo $ _GET [id]. "<br>"; // 1
echo $ _GET [action]; // 2?>
[/ pre]

[pre]

<? php
// pseudo-static method two
// localhost / php100 / test.php / 1/2 $ filename = basename ($ _ SERVER ['SCRIPT_NAME']);
echo $ _SERVER ['SCRIPT_NAME']. "<br>"; // /php100/test.php echo $ filename. "<br>"; // test.php
if (strtolower ($ filename) == 'test.php') {
if (! empty ($ _ GET [id])) {$ id = intval ($ _ GET [id]);
echo $ id. "<br>"; $ action = intval ($ _ GET [action]);
echo $ action. "<br>";} else {
$ nav = $ _ SERVER ['REQUEST_URI']; echo "1:". $ nav. "<br>"; // /php100/test.php/1/2
$ script = $ _ SERVER ['SCRIPT_NAME']; echo "2:". $ script. "<br>"; // /php100/test.php
$ nav = ereg_replace ("^ $ script", "", urldecode ($ nav)); echo $ nav. "<br>"; // / 1/2
$ vars = explode ("/", $ nav); print_r ($ vars); // Array ([0] => [1] => 1 [2] => 2)
echo "<br>"; $ id = intval ($ vars [1]);
$ action = intval ($ vars [2]);}
echo $ id. '&'. $ action;}
?>
[/ pre]


[pre]

<? php
// pseudo-static method three
function mod_rewrite () {
global $ _GET; $ nav = $ _ SERVER ["REQUEST_URI"];
echo $ nav. "<br>"; $ script_name = $ _ SERVER ["SCRIPT_NAME"];
echo $ script_name. ""; $ nav = substr (ereg_replace ("^ $ script_name", "", urldecode ($ nav)), 1);
$ nav = preg_replace ("/ ^. ht (m) {1} (l) {0,1} $ /", "", $ nav); // The phrase is Remove the tail of .html or .htm
echo $ nav. ";"; $ vars = explode ("/", $ nav);
print_r ($ vars); echo "<br>";
for $ i = 0; $ i <Count ($ vars); $ i + = 2) {$ _GET ["$ vars [$ i]"] = $ vars [$ i + 1];
} return $ _GET;
} mod_rewrite ();
$ year = $ _ GET ["year"]; // the result is '2006' echo $ year. "<br>";
$ action = $ _ GET ["action"]; // result is '_add' echo $ action;
?>
[/ pre]


[pre]

<? php
// pseudo-static method four
// Use the server variable to get the PATH_INFO information In this example, /1,100,8630.html is the portion of the script that executes if (@ $ path_info = $ _ SERVER ["PATH_INFO"]) {
// Regularly match the parameters if (preg_match ("// (d +), (d +), (d +). Html / si", $ path_info, $ arr_path)) {
$ gid = intval ($ arr_path [1]); // get value 1 $ sid = intval ($ arr_path [2]); // get value 100
$ softid = intval ($ arr_path [3]); // Get value 8630} else die ("Path: Error!");
// equivalent to soft.php? Gid = 1 & sid = 100 & softid = 8630} else die ('Path: Nothing!');
?>
[/ pre]

Related Article

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.