Kryptix Posted November 19, 2009 Share Posted November 19, 2009 In the past I've had variables clash. For example: <?php $c = 5; $cat = "Molly"; $echo $cat . " is " . $c . " years old."; ?> Before, I've seen it do: "5at is 5 years old." Is there anyway around this? Quote Link to comment https://forums.phpfreaks.com/topic/182101-variable-clash/ Share on other sites More sharing options...
premiso Posted November 19, 2009 Share Posted November 19, 2009 I have never seen that. By all logic it should not do that...I am interested to test this out and see what comes of it, too bad this computer isn't a dev one =\ As for that example, I think you want echo and not $echo Just out of curiosity, what version of php and apache are you using? Quote Link to comment https://forums.phpfreaks.com/topic/182101-variable-clash/#findComment-960655 Share on other sites More sharing options...
Alex Posted November 19, 2009 Share Posted November 19, 2009 That shouldn't be happening. Is that the exact code that you're seeing this error with? I suspect not because you have a parse error there.. $echo $cat . " is " . $c . " years old."; Should be echo... (no $) Quote Link to comment https://forums.phpfreaks.com/topic/182101-variable-clash/#findComment-960657 Share on other sites More sharing options...
mikesta707 Posted November 19, 2009 Share Posted November 19, 2009 I've never seen that happen before in my life to be completely honest. Btw, you should have your echo line be echo ... not $echo ... that won't even run... will give a parse error. I actually just tested that, and it worked perfectly fine. Is there something special you did that I can do to try to recreate the error? edit: beaten Quote Link to comment https://forums.phpfreaks.com/topic/182101-variable-clash/#findComment-960658 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 it works for me to just do: $var = "$cat is $number years old."; Quote Link to comment https://forums.phpfreaks.com/topic/182101-variable-clash/#findComment-960666 Share on other sites More sharing options...
premiso Posted November 19, 2009 Share Posted November 19, 2009 it works for me to just do: $var = "$cat is $number years old."; I think you missed the point of the post, he seems to be getting overlap when he used $c and his theory is that the variables are messing up cause $cat has a c in it...which I have never seen happen Quote Link to comment https://forums.phpfreaks.com/topic/182101-variable-clash/#findComment-960951 Share on other sites More sharing options...
Deoctor Posted November 19, 2009 Share Posted November 19, 2009 hai if i am correct u might have used the echo as of below <?php $c=5; $cat="Molly"; echo $c."at is ".$c." years old."; ?> this is the only way u can get this error.. Quote Link to comment https://forums.phpfreaks.com/topic/182101-variable-clash/#findComment-960958 Share on other sites More sharing options...
Maq Posted November 19, 2009 Share Posted November 19, 2009 hai if i am correct u might have used the echo as of below $c=5; $cat="Molly"; echo $c."at is ".$c." years old."; ?> this is the only way u can get this error.. Firstly, that doesn't throw and error. Second, I'm not sure you guys are understanding the whole point. As premiso previously mentioned: I think you missed the point of the post, he seems to be getting overlap when he used $c and his theory is that the variables are messing up cause $cat has a c in it...which I have never seen happen Quote Link to comment https://forums.phpfreaks.com/topic/182101-variable-clash/#findComment-961063 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 thanks for poining that out premiso! well in that case! i can tell you it HAS HAPPENED TO ME! thats why u have to sometimes in those tricky situations. echo $c; and then you know echo them sepeaterly Quote Link to comment https://forums.phpfreaks.com/topic/182101-variable-clash/#findComment-961235 Share on other sites More sharing options...
Kryptix Posted November 21, 2009 Author Share Posted November 21, 2009 Yes, it has happened to me in the past and just happened to me again. No, that wasn't the exact code, I just quickly typed it to give a simple example. Excuse the error, I was in a rush (yeah, that's what they all say). It just happened using this code... function encode_username($username) { $username = strtolower($username); $clean = ''; for($i = 0;$i < strlen($username);$i++) { $c = ord($username{$i}); if($c >= 97 && $c <= 122) { $clean .= chr($c); } else if($c >= 48 && $c <= 57) { $clean .= chr($c); } else { $clean .= ' '; } } $clean = trim($clean); if(strlen($clean) > 12) { $clean = substr($clean, 0, 12); } $hash = '0'; for($i = 0;$i < strlen($clean);$i++) { $c = ord($clean{$i}); $hash = bcmul($hash, 37); if($c >= 97 && $c <= 122) { $hash = bcadd($hash, (1 + $c) - 97); } else if($c >= 48 && $c <= 57) { $hash = bcadd($hash, (27 + $c) - 48); } } return $hash; } My friend wrote a form script that got their character name as a variable $cname. We couldn't see what the problem was, but after doing a Find & Replace (automatically) from $cname to $bname, it suddenly worked. We could only presume that the $c in the external function (which has been included) was overlapping and causing problems. I had this happen to me ages ago, but I took a long time away from PHP and only recently came back... But yeah, I'm 99.9% sure it does happen under some circumstances! Quote Link to comment https://forums.phpfreaks.com/topic/182101-variable-clash/#findComment-962326 Share on other sites More sharing options...
Kryptix Posted November 21, 2009 Author Share Posted November 21, 2009 PS. This is on a Windows Server 2003 machine running the latest XAMPP, but this happened to me years ago on shared Linux hosting too. I've always been really paranoid of variables and called them very tricky names just to make sure they don't clash with each other. Quote Link to comment https://forums.phpfreaks.com/topic/182101-variable-clash/#findComment-962327 Share on other sites More sharing options...
Alex Posted November 21, 2009 Share Posted November 21, 2009 What in that code do you think is clashing? Quote Link to comment https://forums.phpfreaks.com/topic/182101-variable-clash/#findComment-962328 Share on other sites More sharing options...
Kryptix Posted November 21, 2009 Author Share Posted November 21, 2009 Well, we had a variable called: $cname = "Kryptix"; We'd then remotely call that function... encode_username($cname); ...and it'd give us really weird results. We changed it to $bname and it worked fine. I presume the $c in $cname was causing the problems, as I've seen it happen before. Quote Link to comment https://forums.phpfreaks.com/topic/182101-variable-clash/#findComment-962401 Share on other sites More sharing options...
premiso Posted November 21, 2009 Share Posted November 21, 2009 Just a question, is register_globals turned on? Quote Link to comment https://forums.phpfreaks.com/topic/182101-variable-clash/#findComment-962484 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.