Display dynamic text based on GET parameters

I am trying to learn PHP, and I decided that I would do myself a simple exercise to create a site that, if someone goes to it, will receive "Hello, friend!" . but if my wife (called Dawn) comes up to her, she gets another message.

Unfortunately, it always appears as empty, and I'm not sure why.

I know that it works for index.html with text only, and I know that it works for index.php if there is no tag in it <?php(only text works). But when I try to make it an actual php, it just fails.

  • I would like to site/index.php "Hello friend!"
  • I would like to site/index.php?who=Bob "Hello friend!"
  • I would like to say site/index.php?who=Dawn "Hello, dawn! I love you!"

Here is what I have:

<?php
print 'Hello ';
$who = $_GET("who");
if($who && $who == "Dawn")
    print "Dawn! I love you!";
else
    print "friend!";
/>

So what's wrong?

+3
source share
3 answers

Accessing arrays (which $_GETis an array), like in Java, uses square brackets:

$who = $_GET['who'];

Also if($who)evaluates to true, if $whonot false, to check it you need to use isset:

if(isset($who) && $who == "Dawn")

Finally, as @Shivan pointed out, the end tag should be ?>, not />.

+7
source

A few problems:

<?php
print 'Hello ';
$who = $_GET["who"];
if(isset($who) && $who == "Dawn") {
    print 'Dawn! I love you!';
} else {
    print 'friend!';
}
?>

Additional Information:

  • use single quotes if possible for faster parsing
  • It's a good habit to close if else with parentheses
  • The end tag must be ?>
+3
source

:

<?php
echo 'Hello ';
$who = isset($_GET["who"])?$_GET["who"]:false;
if($who)
    echo "Dawn! I love you!";
else
    echo  "friend!";
?>

, _GET who, php .

+1

All Articles