undefined variable
Posted by: vdolnik1 (---.hfc.comcastbusiness.net)
Date: June 27, 2009 09:18AM

I want to switch from my rather outdated apache/php/mySQL to Wampserver, but have a problem, that does not exist in my current system:

When testing Wampserver on local host, I got URL like this:

[127.0.0.1]

The file index.php contains

<?php

if ($ID=="main" ){
echo "$ID";
} else {
echo "empty ";
}
?>

I always obtain reply "empty". What is wrong? As php should be compatible backwards, I assume a problem in php.ini settings but could not find anything wrong. Under Apache 1.3.23 there are no problems.

Thank you for any helping suggestion.

Vlad Dolnik

Options: ReplyQuote
Re: undefined variable
Posted by: stevenmartin99 (---.b-ras2.blp.dublin.eircom.net)
Date: June 27, 2009 10:03AM

little do you kinow about php if u think its backwards compatible. its harder ever backwards compatable!!



<?php

$ID = $_GET['main'];

if ($ID=="main" ){
echo $ID;
} else {
echo "empty ";
}
?>


you will need this new line as php now has GLOBAL variables turned off and you shud not turn it on..
you need to tell php you want the variable and how it should be gotten...

$_GET $_POST $_REQUEST


also echo "$id"; will print $id so just use echo $ID;



if you get a undefined varibale error if you dont have ?id=main that is beacuse php couldnt declare $id for the rest of the script . you should look up "isset" on PHP.NET to see how to check if a variable is set before you use it


-- lots have chnage since a few versions ago smiling smiley

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: undefined variable
Posted by: vdolnik1 (---.hfc.comcastbusiness.net)
Date: June 27, 2009 08:10PM

Steven,

you are right, lots have changed. My train is gone so I bought a book on PHP 5 by J. Meloni. Unfortunately, it does not seem to differ much from the previous edition, which I appreciated seven years ago. I cannot find answers to some of my trivial questions:

1) My php code works differently in a php file and a php section of a htm file. Is it a standard? Should I prefer php files? Or is something wrong in my settings?

2) If I disable global variables and long arrays how can I get the name of the page from a link to a php code so that I can use

if ($ID=="main" ){include "../main.php";}

when having a link to ../localhost/default.htm?ID=main. Is there another simple way?

Best regards,

Vlad

Options: ReplyQuote
Re: undefined variable
Posted by: c2dan (---.15-1.cable.virginmedia.com)
Date: June 27, 2009 10:09PM

> 1) My php code works differently in a php file and
> a php section of a htm file. Is it a standard?
> Should I prefer php files? Or is something wrong
> in my settings?

By default WAMP configures Apache to only parse PHP code within .php files. If so you wish you can modify Apaches configuration to parse PHP code within other files, such as .html

To do so left click WAMP's tray icon and select Apache > httpd.conf. Scroll down to line 386 which should be this line
AddType application/x-httpd-php .php

Add a space after .php and type in .html (or any other file extension for PHP code to be parsed in).

Save the httpd.conf and restart the Apache service.

> 2) If I disable global variables and long arrays
> how can I get the name of the page from a link to
> a php code so that I can use
>
> if ($ID=="main" ){include "../main.php";}

If register_globals is disabled then you need to use the correct superglobal variable for where the variable ($ID) is coming from.

For example if the $ID is coming from the url (as in filenmame.php?ID=somevalue) then use the $_GET superglobal. So rather than using $ID you'd use $_GET['ID'] instead.

Similarly if your variable is the name of a form field you'd use $_POST['ID']

For more info read the PHP documentation about superglobals
[uk2.php.net]

Options: ReplyQuote


Sorry, only registered users may post in this forum.