 |
| Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 19th, 2005, 02:34 PM
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
php _get and undeclared variables
Greetings,
Still rather new at all this and undoubtedly in over my head.
I am getting "undeclared variable" error messages on the following code, which is located in the head section of the page. I get an error on "image", "h", and "w".
<?
$image=$_GET["image"];
$height=$_GET["h"];
$width=$_GET["w"];
?>
The page displays properly following the error message.
I use this to display a larger version of a selected photograph, using thumbnails as links.
<a href="photos1.php?image=pic10&w=500&h=375" ><img src="photos/thumb10.jpg" width="100" height="75" alt="" /></a>
followed by
<?
if ($image == '') {
echo "<h3>Click a thumbnail<br />";
echo "to view the pictures.</h3>";
}
else {
echo "<img src=\"photos/$image.jpg\" width=\"$width\" height=\"$height\"/>";
}
?>
I tried changing the register global just to see if that was the problem, but it didn't matter.
How can I satisfy the error message?
Thanks
Robert
|
|

January 19th, 2005, 11:05 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Posts: 117
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
hi Wolferd1
<a href="photos1.php?image=pic10&w=500&h=375" >
I think the problem is amp;w=500&h=375 I think you cannot use ;.
try delete them from your code and let see if it works or not
|
|

January 20th, 2005, 12:15 AM
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
mani_he,
Thanks for taking a look. I tried it with and without the semicolons, it didn't make any difference. Apache logs the errors like this:
[Wed Jan 19 23:07:27 2005] [error] PHP Notice: Undefined index: image in c:\program files\easyphp1-7\www\fjpmwebpage\photos1.php on line 15
[Wed Jan 19 23:07:27 2005] [error] PHP Notice: Undefined index: h in c:\program files\easyphp1-7\www\fjpmwebpage\photos1.php on line 16
[Wed Jan 19 23:07:27 2005] [error] PHP Notice: Undefined index: w in c:\program files\easyphp1-7\www\fjpmwebpage\photos1.php on line 17
Those line number correspond to these three lines
$image=$_GET["image"];
$height=$_GET["h"];
$width=$_GET["w"];
But once you click on one of the links and the line is executed and the variables are assigned values, the errors go away until you reload the original page. It obvious that the error comes only when "image", "h", and "w" have no value. Once you start executing them, the problem disappears.
Thanks for checking,
Robert
|
|

January 20th, 2005, 11:12 AM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
do this
$image=@$_GET["image"];
$height=@$_GET["h"];
$width=@$_GET["w"];
|
|

January 20th, 2005, 11:13 AM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
or set your error reporting to 0
|
|

January 20th, 2005, 05:09 PM
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Gotaka4,
I already have the error reporting level set so that the errors are not recorded in the Apache log. But when I look at the page locally (that's all I am using it for, I'm not serving any pages) the error messages appear on the page I am looking at anyway, at the point in the page where the code shows up.
I have no idea what adding the @ will do, but I will give it a try tonight or tomorrow. Thanks for the suggestion.
wolferd
|
|

January 20th, 2005, 05:36 PM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
the @ will repress error reporting...in your case, it should solve your problem. :-)
|
|

January 21st, 2005, 11:47 AM
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Gotaka4
Followed your suggestion
"do this
$image=@$_GET["image"];
$height=@$_GET["h"];
$width=@$_GET["w"];"
I no longer get the message. It makes it a lot easier to work on the page. Thanks.
My only misgiving is that I have always worked under the assumption that when there is an error you correct the error rather than disable the reporting of that error. I have seen a lot of postings where people are having problems with the same error message. Is there actually an error, or is this something to do with some kind of PHP bug?
I like not seeing the message, but I keep wondering why the message is generated in the first place.
Thanks again
wolferd
|
|

January 21st, 2005, 03:36 PM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
the error is generated, as the error messages state, because the variables aren't defined on that page when its being loaded. When you have a $_GET request, you want to make sure that you pass it before requesting that variable.
You can, however, do this:
if(!isset($_GET['image'])){
$image = '/path/to/file' //some default value: when the page just loads.
}
But other than that there is not other way to supress it except for putting an @ sign.
|
|

January 22nd, 2005, 11:51 PM
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Gotaka4,
I appreciate your patience, and I won't drag this thread out any longer after I ask one question. YOu say
Quote:
|
quote:the error is generated, as the error messages state, because the variables aren't defined on that page when its being loaded. When you have a $_GET request, you want to make sure that you pass it before requesting that variable.
|
The $_GET and any other variables that I am using appear only on this page. I thought php variables were defined/declared when they were used. What have I left out? Maybe it is the details of your comment
Quote:
|
quote:you want to make sure that you pass it before requesting that variable.
|
that I need to try and understand.
Thanks again for your help.
wolferd1
|
|
 |