View Full Version : Wierd variable problem.

08-30-2007, 03:24 PM
$lightning_def0 = $gen['lightning_def0'];

^^ This is a variable i get from my database

if($lightning_def0 == "ALL") {
$p1_w1_def_lightningicons = "ALL";
}else {
$p1_w1_def_lightningicons = rand($lightning_def0 , $lightning_def1);

This works fine when $lightning_def0 is a number from 1-10. But when i enter 0 it automatically assumes 0 is ALL. Which is wierd =/.

08-30-2007, 03:43 PM
Please have a look at the recent thread
2 variables with the same string doesn't match. (http://www.codingforums.com/showthread.php?t=122591)
You can't compare strings with "==", instead use strcmp()

08-30-2007, 03:50 PM
You can compare strings with "==", however if they aren't the same it will return false. If there's any chance the string won't be the same case use strtoupper() or strtolower() before comparing them.

If you need to compare them based on their type use "==="

09-02-2007, 04:49 PM
When you compare a number with a string like:
$x = 0;
$y = 'Hello World';

$xIsY = ($x == $y);the string will be converted to a number using the usual conversion rules.

That means in your case "ALL" will be converted to 0 (zero), which of course is equal to 0.

If you don't want this to happen cast $lightning_def0 to a string before comparing.


EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum