Web Hosting Talk







View Full Version : my defined variables are undefined?!


colossus
08-11-2004, 12:00 AM
I am making a script that takes the options selected by a person and then submits them to paypal to the order form. The problem is, I have multiple switch statements which define variables, do some math, and then echo a bunch of info with those variables. Problem is, some of my variables weren't reporting. I turned on error reporting all the way and it said that a series of my variables (all of which weren't returning) were undefined!. Here is a snippet of the code.

<?php
$beginnerbutton = "
<center>You have selected the Beginner Hosting Plan and " . $l . " for your payment length. <a href='hosting.html'>Change?</a><br /><br /></center><center><form action='https://www.paypal.com/cgi-bin/webscr' method='post'>
//OMITTED
<input type='hidden' name='a3' value='" . $amount . "'>
<input type='hidden' name='p3' value='" . $p3 . "'>
<input type='hidden' name='t3' value='" . $t3 . "'>
//OMITTED
";
//other variables like $beginnerbutton omitted
$l = $_GET['l'];
$q = $_GET['q'];
switch ($q){
case "beginner":
$plancost = 1.00;
sl();
echo $beginnerbutton;
break;
//other cases like beginner omitted
};
function sl(){
switch ($l){
case "Monthly":
$amount = $plancost * 1;
$p3 = 1;
$t3 = "M";
break;
//other cases like Monthly omitted
};
};


?>



Here are the errors I'm getting:

Notice: Undefined variable: amount in /home/percept/public_html/signuptest.php on line 95

Notice: Undefined variable: p3 in /home/percept/public_html/signuptest.php on line 96

Notice: Undefined variable: t3 in /home/percept/public_html/signuptest.php on line 97

Notice: Undefined variable: l in /home/percept/public_html/signuptest.php on line 167

And all of them are returned three times. The line numbers don't matter.

Rich2k
08-11-2004, 02:19 AM
Change your error reporting to better mode. E_ALL has a habit of reporting more than it needs to.

Change to

error_reporting(E_ALL ^ E_NOTICE);

Kangar00
08-11-2004, 05:18 AM
take a closer look at your function sl():
you need to define $l, $amount, $p3 and $t3 as global if you want to access the respective global vars in the function. Otherwise they are local to the function!

wimvds
08-11-2004, 07:22 AM
And on top of what is mentioned above by kangar00, you already use the variables in your $beginnerbutton assignment, when those vars haven't been set yet. So you'd better move the $var=$_GET['var']; statements so that they are interpreted before you use these vars in an assignment...

ie.

<?php
$l = $_GET['l'];
$q = $_GET['q'];

$beginnerbutton = "
<center>You have selected the Beginner Hosting Plan and " . $l . " for your payment length. <a href='hosting.html'>Change?</a>...";

Burhan
08-11-2004, 08:11 AM
Two things you need to know :

1. Variable scope (http://www.php.net/manual/en/language.variables.scope.php)

2. How to define functions (http://www.php.net/manual/en/language.functions.php).

There is no ; at the end of a } that closes a function.