How to have the isset function using $ queryString in php

3

I have a code in php that has the function to get the parameter ?capa= with parse_str

<?php 
$url = parse_url($_SERVER["HTTP_REFERER"]);
parse_str($url["query"],$queryString);?> 

This is his code: <?php echo $queryString["capa"] ?>

but how to do the isset function of $_GET in $queryString

That is, with $ _GET I would do this: $largura = isset($_GET ["largura"])?$_GET ["largura"]:"100%"; ie if it did not have a value in ?largura= would receive 100%.

how to do this another way since the query string is interpreted with parse_str ?

    
asked by anonymous 23.04.2018 / 08:52

1 answer

3

For what you indicated in the comments you are using parse_url and parse_str to get the query string parameters of a given url. If you check the documentation you see that parse_str returns you an array with key and value for each parameter in the query string.

If you want to know if it does not exist to assign a default value, you can use isset in it, querying for the key you want:

$capa = isset($queryString["capa"]) ? $queryString["capa"] : "100%";

Or even straight into echo :

<?php echo isset($queryString["capa"]) ? $queryString["capa"] : "100%"; ?>

You can even use null coalescing operator to do the same if you are running php version 7:

$capa = $queryString["capa"] ?? "100%";

E

<?php echo $queryString["capa"] ?? "100%"; ?>
    
23.04.2018 / 12:33