Jump to content

Archived

This topic is now archived and is closed to further replies.

johnnyk

One of the strangest things...ever

Recommended Posts

PHP Version 5.1.4

This is weird and annoying and pissing me off.

if('2006' == '2005'){
  echo "<div class=\"class\">\n";
  echo "<h5 class=\"class\">text</h5><.br />\n";
  echo "</div>\n";
}

(I added the . to the <.br /> because this new forum is evil)

The if executes. Am I missing something obvious? Honestly, what the curse? This is the strangest PHP related problem I've ever had. Honestly, at this point if you told me it was executing because I'm not standing on my head I would believe you because I can't find any other reason.

I had another weird problem the other day. MySQL kept saying there was an error in my SQL syntax when there wasn't. I even posted it on here and someone verified it was fine with the version of MySQL I was using, but MySQL kept bitching.

So my question is, why do PHP and MySQL hate me? Or maybe my question is, why am I going crazy? This is like a PHP nightmare. Things are just going crazy for absolutely no reason at all. AHH!!!!

Share this post


Link to post
Share on other sites
Try this you dont need to back slash while using double quotes do ya lol?

if('2006' == '2005'){
   echo "<div class='class'>";
   echo "<h5 class='class'>text</h5><br>";
   echo "</div>";
}

Also when you got the " double used then in the middile you use  ' single , so double then single lol

Share this post


Link to post
Share on other sites
yes you do need to use backslashes with double quotes, if you started out with double quotes.  but that's not the point. the point is that php is deeming that if statement true. honestly i have no clue why...

Share this post


Link to post
Share on other sites
I tested it this way and works fine.

<?
$a="2005";
$b="2006";

if($b>$a){
   echo "<div class='class'>";
   echo "<h5 class='class'>text</h5>
";
   echo "</div>";
}
?>


this also works fine

<?
$a="2005";
$b="2006";

if($b==$a){
  echo "<div class='class'>";
  echo "<h5 class='class'>text</h5>
";
  echo "</div>";
}
?>

Share this post


Link to post
Share on other sites
It only gets screwed up in a certain script of mine. If I isolate the if into its own php script it works fine.

Is there any possibility that somehow the rest of my script is affecting the if? Is there any possibility it's a really strange bug? Is there any possibility my host is to blame?

Something is cursed up here, but I have no idea what.

Where's the smiley for "Head is about to explode"? Damn new forum.

Share this post


Link to post
Share on other sites
so if you:

if('2006' != '2005'){
  echo "<div class=\"class\">\n";
  echo "<h5 class=\"class\">text</h5><br />\n";
  echo "</div>\n";
}

does it display nothing  :o

and redarrow,

yes you do need need to escape double quotes in double quoted strings its single quoted strings that you do not need to escape double quotes.

Share this post


Link to post
Share on other sites
umm.. i guess post the rest of your code that surrounds it, cuz i tried it in an isolated example for shits and grins and it worked the way it should...

Share this post


Link to post
Share on other sites
...um, sorry people. It actually was working fine...
I saw something coming up on the page and got confused and thought it was whats in the if...
...sorry to waste your time

I'm an idiot. But it's late at night, so you can't blame me too much.

Share this post


Link to post
Share on other sites
Crayon Violent a quick quistion how comes the code i posted in reply worked when it shouldnt as i have not added any \backslashes help lol.


I see the ansaw know its becouse the oreganal code were the class has double qoutes are there and mine are single ok

bla bla ...............

Share this post


Link to post
Share on other sites
because you edited your post to reflect single quotes inside the double quotes, your original post would have terminated the strings and caused an error.

Share this post


Link to post
Share on other sites
thank you.

i got told from my teacher not to use double qoutes within double qoutes

so does \escaping double qoutes work faster or is it a coding prefrence cheers.

Share this post


Link to post
Share on other sites
it depends on the circumstance.  if you were using quotes in a normal sentence structure, for example:

The lady said, "How may I help you?"

You would want to preserve the double quotes, so you would escape them:

echo "The lady said, \"How may I help you?\"";

When dealing with variables in a string, most the time it is easer to use double quotes. Why? because if you do this:

[code=php:0]
$blah = "foobar";
echo "$blah";
echo '$blah';
[/code]

The first echo will echo foobar, where as the 2nd echo will literally echo $blah.  So, if you are, for instance, building a query string, well, values require quotes around them, in order for them to be valid in mysql. well, string values do, not numerical.  But anyways, which do you think looks cleaner, this:

$sql = "update table set blah = \"".$blah."\" where id = \"". $id . "\"";

or this:

$sql = "update table set blah = '$blah' where id = '$id'";

Share this post


Link to post
Share on other sites

thanks for the reply.

this

$sql = "update table set blah = '$blah' where id = '$id'";

Share this post


Link to post
Share on other sites
You don't need to quote integers, its bad practice for a start, eg if PHP wasn't so lenient, such as C for example, trying to compare strings as you were (arrays of characters) would result in many errors.

Bad Practice:
[code]if('2006' != '2005')[/code]
Bad Practice:
[code]if("2006" != "2005")[/code]
Good Practice:
[code]if(2006 != 2005)[/code]

And using quotes inside your strings will NOT slow down the parsing of your script...

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.