Php with no arguments?

just wondering. I saw sites with this type of URL http://www.something.com/?somedata with "somedata" - this is the value of some variable "unmentioned"

how can i do something like that? all i know is the traditional http://www.something.com/index.php?arg=somedata

Many thanks

+3
source share
7 answers

It is just a variable with no value. You can get this string using list($value) = array_keys($_GET);- if you guaranteed that $_GETthere is exactly one value ( count($_GET) === 1) in the array , otherwise you will receive an error message (if $count === 0) or unwanted behavior (if $count > 1).

+4

, $_SERVER["QUERY_STRING"].

, - , 100% Apache IIS.

+1

.

somedata somedata = 1 somedata = true

, , .

if ( isset($_GET['somedata']) ) { 
    //do something
}
+1

, " " ", . , foo=bar&baz, foo bar baz ( PHP ).

, . $_GET, key. , key NULL.

?somedata=somevalue

var_dump(key($_GET), $_GET);
/*
string(8) "somedata"
array(1) {
  ["somedata"]=>
  string(9) "somevalue"
}
*/

?somedata

var_dump(key($_GET), $_GET);
/*
string(8) "somedata"
array(1) {
  ["somedata"]=>
  string(0) ""
}
*/

?

var_dump(key($_GET), $_GET);
/*
NULL
array(0) {
}
*/
+1

URL- $_GET:

foreach ($_GET as $key => $value) {
    if ($key == 'somedata') {
        //do something
    }
}

$_GET.

$_GET array_keys($_GET).

0

-, , . , http://example.com/, ( index.html). PHP index.php , . Apache:

http://httpd.apache.org/docs/2.2/en/mod/mod_dir.html#directoryindex

?somedata, (, , - ). , , ​​ :

<?php

if( isset($_GET['somedata']) ){
    // Do some stuff
}

?>
0

Source: https://habr.com/ru/post/1743129/


All Articles