-
php script help...
Hello,
I have the following php script:
test.php:
Code:
<?
if ($name) {
echo $name;
}else{
echo "No Page Name";
}
?>
and if you goto mysite.com/test.php?name=hello
it should display the text 'hello' on the page where the script is put but it don't work.
does anyone know why this is not working for me?
When i run it, it comes up with 'No Page Name' rather then what I put in the URL
My web server is:
Apache version: 2.2.6 (Unix)
PHP version: 5.2.5
Hope someone can help
Thanks.
-
Re: php script help...
You haven't shown how you are getting the $name variable, so we can't tell you anything concerning that. However, when checking for variables, you would use isset(). PHP isn't like most other languages when checking variables.
PHP Code:
<?php
if (isset($name)) {echo $name;}
else {echo "No Page Name";}
?>
-
Re: php script help...
the URL does not set the variable itself. You need to pull it out of the $_POST or $_GET array first.
In this case you are using GET.
Code:
<?php
if (isset($_GET['name'])){
$name = $_GET['name'];
echo $name;
}else{
echo "No Page Name";
}
?>
-
Re: php script help...
Thanks, I ended up using:
Code:
<?php
if (isset($_GET['name'])){
$name = $_GET['name'];
echo $name;
}else{
echo "No Page Name";
}
?>
I thought i was missing something in the code.
Thanks.
-
Re: php script help...
I assume you might do more than just displaying one line of text with the variable $name.
So better start with
Code:
$name=isset($_GET["name"])?$_GET["name"]:"No name";
echo $name;
There is a way you can do it the way of your first code, with setting register_globals on, but this is a major security risk to use.