tags:

views:

79

answers:

2

Can you tell me your opinion of this, and possibly see if you can recreate it:

Currently, the $post->post_content variable contains:

"before <img src="/path/to/valid_img.gif" /> after"

This code placed at the top of the theme header.php...

------ Code --------
    1: $str = $post->post_content;
    2: assert( isset( $str ) );
    3: assert( is_string( $str ) );
    4: echo $str;
    5: $str = 'before <img src="/path/to/nonexistant.gif" /> after';
    6: assert( isset( $str ) );
    7: echo $str;
--------------------

Outputs this...

------ Output ------
before <img src="/path/to/valid_img.gif" /> after
before <img src="/path/to/nonexistant.gif" /> after
--------------------

With these warnings...

--- PHP Warnings ---
PHP Warning:  assert() [<a href='function.assert'>function.assert</a>]: Assertion
failed in /path/to/header.php on line 2
PHP Warning:  assert() [<a href='function.assert'>function.assert</a>]: Assertion
failed in /path/to/header.php on line 3
--------------------

Why would line 4 properly echo the $str if assert() fails twice when I KNOW 100% it should succeed?

What does a broken image src have to do with setting or unsetting the variable $str? WordPress bug/oddity?

Now the crazy part...

When lines 5-7 are commented out, thus eliminating the nonexistant.gif, the assert() warning does NOT appear and the output STILL properly produces this...

------ Output ------
before <img src="/path/to/valid_img.gif" /> after
--------------------

Any chance you can reproduce this and tell me what you think? I'm new to PHP, but I'm pretty sure this is crazyness. :)

A: 

assert() makes no sense in your code, what you want is an if statement.

smoove666
Really? I thought assert() was used for debugging?
Jeff
better use var_dump(isset($str)), this way you see the return value of isset(), not the returnvalue of assert()
smoove666
assert() from the php manual: http://de.php.net/assert , assertion is not what you want, you want to know if isset() returns true, you can check this via var_dump() as i said above, assert() is not meant for this task.
smoove666
Thanks smoove, I'm reworking my problem now. Much appreciated.
Jeff
you're welcome :)
smoove666
Jeff was using assert because he wanted to see errors in the log in sequence with the PHP warning he was getting... FYI...
Josh
A: 

Shouldn't you give assertion as a string?

Kuroki Kaze
According to my code above, unless I'm mistaken about the purpose of assert(), it appears to be doing its job.
Jeff
All i want to say is try to turn assert parameters into string. It seems that `assert(TRUE)` will lead to exactly your error.
Kuroki Kaze
`assert( 'isset( $str )' );`
Kuroki Kaze
Got it, got it! I'm reworking my code now... Thanks!
Jeff
I suppose 'assert' is in place here - sort of defensive programming in case $post->post_content will contain some garbage instead of string. Why it's failing is a different question :)
Kuroki Kaze
How do I mark a question as no longer needing an answer? lol
Jeff
There must be "close" button near "edit" in the question. I suppose you should mark one of answers as accepted to do that, but maybe not.
Kuroki Kaze