How to have isset function using $queryString in php

Asked

Viewed 79 times

2

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

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

That and his code: <?php echo $queryString["capa"] ?>

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

I mean, with the $_GET I would do this: $largura = isset($_GET ["largura"])?$_GET ["largura"]:"100%"; that is if it had no value in ?largura= would receive 100%.

how to do this other way once the query string is interpreted with parse_str ?

  • I couldn’t understand the problem. $largura = isset($_GET ["largura"])?$_GET ["largura"]:"100%"; doesn’t work ?

  • Yes it works, but this using $_GET, in my case I want to do using <?php echo $queryString["capa"] ?>

  • But $queryString is an array ? Or you want to fetch capa also to the parameters of $_GET ? $capa = isset($_GET["capa"]) ? $_GET["capa"] : "outro_valor_que_dê_jeito"; ? Your question is confused.

  • I this to get the information that comes from a site with reference: <?php $url = parse_url($_SERVER["HTTP_REFERER"]);parse_str($url["query"],$queryString);?> so I get the cover reference <?php echo $queryString["capa"] ?> but what if I don’t have a cover, how to put a pattern ? and that’s what I try to talk about..

  • All that you are using to interpret the values is relevant and should be in the question, otherwise it is not clear enough. Add this information to the question.

1 answer

3


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

If you want to know if there is no default value you can use isset in the same, by consulting for the key you want:

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

Or even straight on echo:

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

You can even use the null coalescing Operator(Null coalescence operator) to do the same, if running in php version 7:

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

And

<?php echo $queryString["capa"] ?? "100%"; ?>
  • Thank you very much! helped me! and I didn’t know it was that simple.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.