-2

I want to show a string of text only if the Name URL parameter exists. If it does not, then something else should be shown. However, when I try to do it, I get a parse error. What am I doing wrong?

<?php   
$url = 'http://www.example.com/page/?type=software&sortby=title&Name[]=software%3A%20windows&Name[]=version%3A%2010&sortdirection=asc&Name[]=make%3A%20microsoft';
$url_components = parse_url($url); 
parse_str($url_components['query'], $params); 
$url22 = implode(", ", $params['Name']);
if(!empty($url22)){
echo '<h1>no string</h1>'
}
else {    
echo '<h1>yes string</h1>'
}
?> 
Mike
  • 947
  • 2
  • 12
  • 18
  • You should have said i am getting a parse error like: "Parse error: syntax error, unexpected '}', expecting ';' or ',' in ..... on line 8" – Luuk Aug 23 '20 at 13:57
  • The parse error will tell you exactly what you're missing and where, so please just read it. IIRC you need semicolons after 'echo' so that's probably what it is – cry0genic Aug 23 '20 at 13:58
  • Which simply means you are missing a trailing ';' on line `echo '

    .....`

    – Luuk Aug 23 '20 at 13:59
  • Also parameters in an URL start after the '?', and there is no '?' in your url.... – Luuk Aug 23 '20 at 14:01

1 Answers1

0

You've forgotten semicolon for echo lines. To extract "query", you URL must include the question mark. in your example, "http://www.example.com/page/type=software..." needs a question mark after "...page/". And finally, the empty function returns true if the $url22 is blank/empty, I think you don't need the "!" at the condition. Please check below code:

$url = 'http://www.example.com/page/?type=software&sortby=title&Name[]=software%3A%20windows&Name[]=version%3A%2010&sortdirection=asc&Name[]=make%3A%20microsoft';
$url_components = parse_url($url); 
parse_str($url_components['query'], $params); 
$url22 = implode(", ", $params['Name']);
if(empty($url22)){
echo '<h1>no string</h1>';
}
else {    
echo '<h1>yes string</h1>';
}
Reza Fathi
  • 111
  • 4