Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable with IF Statement

Status
Not open for further replies.

Mike3333

Programmer
Jan 18, 2005
32
US
Hi,
I'm passing $Goal from another form. I'm trying to see if the variable has a value of "$". If it does I want to make that value a "0". Otherwise accept the value in $Goal. The script below doesn't work. Any help would be great.

$Goal=$_POST['Goal'];
if ($Goal == "$") {
$Goal ==$_POST['0'];
} else {
$Goal==$_POST['Goal'];
}
 
instead of
Code:
if ($Goal == "$") {
try
Code:
if ($Goal == "\$") {
or
Code:
if ($Goal == '$') {

I think it has something to do with interpolation of the character $

HTH
--Paul

cigless ...
 
Tested both of your solutions and they both still post the "$". Any other ideas?
 
sorry, skimming, my bad

shouldn't use "==" for string values use "eq"
!= (numeric) ne (string)

HTH
--Paul

cigless ...
 
Mike,

use eq for strings
= is assignement and your case will always be true

Code:
$Goal="not a dollar";
if ($Goal="$") {
  print "$Goal";
} else {
  #doesn't matter what you do here, it'll never fire
  #coz the assignment above will always be true
}
HTH
--Paul

cigless ...
 
Thanks for working with me Paul.

I get a parse error with this.

$Goal=$_POST['Goal'];
if ($Goal eq "$") {
$Goal eq "0";
} else {
$Goal=$_POST['Goal'];
}
 
Code:
$Goal=$_POST['Goal'];
if ($Goal eq '$') {
$Goal eq "0";
} else {
$Goal=$_POST['Goal'];
}

Try the above, I think this is what I saw first, you could also try "\$" instead of '$'

Where are you getting the variable $_POST['Goal'], that looks like a PHP variable?
--Paul

cigless ...
 
$Goal=$_POST['Goal'];

Is a PHP variable and it's being passed from another form.

"\$" gives me a parse error and
'$' doesn't change the "$" to a "0"

and ideas??
 
Mike sorry about this, wasn't really paying attention cause I thought it was too easy.
Code:
$Goal=$_POST['Goal'];
if ($Goal eq '$') {
  $Goal [b][COLOR=red]=[/color][/b] "0"; #this DOES need an =
} else {
  $Goal=$_POST['Goal'];
}

= for assignment
== for numeric comparison
!= for numeric comparison
eq for string comparison
ne for string comparison

Hope that's clear now

the $_POST, i wasn't aware that a php variable could be made visible to PHP. I thought they would be working in different memory spaces, I'll have to look into that one, cheers 4 that

--paul

cigless ...
 
This is what actually works... Must be something to do with the field type? Feel like helping me with a null value i'm having trouble with Paul?

$Goal=$_POST['Goal'];
if ($Goal == '$') {
$Goal = "0"; #this DOES need an =
} else {
$Goal=$_POST['Goal'];
}

Here's the problem i'm having with VARCHAR(255). Doesn't work.

$GoalDesc=$_POST['GoalDesc'];
if ($_POST['GoalDesc'] = "") {
$GoalDesc ="No Goal Description";
} else {
$GoalDesc=$_POST['GoalDesc'];
}

FYI, i'm totally new to MySQL and PHP, i'm an ASP/Access,SQL programmer...
 
See how stupid I am... Didn't know that there was a difference!!
 
PHP and perl are similar, but it would be best to ask in the PHP forum to avoid further confusion. Have fun, PHP is pretty easy really, I'm sure you will get the hang of it fast.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top