Affected page
https://www.php.net/manual/en/language.basic-syntax.phpmode.php
Issue description
Here is the code provided for Example #2 Advanced escaping using conditions:
<?php if ($expression == true): ?>
This will show if the expression is true.
<?php else: ?>
Otherwise this will show.
<?php endif; ?>
When running this code, the following warning is printed (along with the text output "Otherwise this will show."):
Warning: Undefined variable $expression in script on line 1
This is correct behavior for the example code, since $expression is indeed undefined. But that isn't related to the behavior the example is supposed to be showing. The purpose of the section is to demonstrate conditional text skipping. This bug in the script only distracts from the intended content.
It's possible that this is intentional (maybe to show the interaction of the text output and the PHP engine's error output?), but the surrounding documentation makes no reference to it, so I'm guessing that it isn't.
I'm very new to PHP, so my apologies if I'm just misunderstanding the documentation and/or script.
Steps to reproduce
- Go to the page: with the example's header highlighted
- Press
Run code underneath Example #2
Suggested fix
Here's my suggestion for how to make this example more helpful:
<?php if (1 == 2): ?>
This will show if the expression is true.
<?php else: ?>
Otherwise this will show.
<?php endif; ?>
This has the same text output as the original and it still demonstrates that an evaluable expression can be used as the predicate of an if-then-else statement to conditionally skip HTML text. At the same time, it doesn't involve the unrelated semantics of comparing values to undefined variables.
Of course, this is by no means the only solution.
Affected page
https://www.php.net/manual/en/language.basic-syntax.phpmode.php
Issue description
Here is the code provided for Example #2 Advanced escaping using conditions:
When running this code, the following warning is printed (along with the text output "Otherwise this will show."):
This is correct behavior for the example code, since
$expressionis indeed undefined. But that isn't related to the behavior the example is supposed to be showing. The purpose of the section is to demonstrate conditional text skipping. This bug in the script only distracts from the intended content.It's possible that this is intentional (maybe to show the interaction of the text output and the PHP engine's error output?), but the surrounding documentation makes no reference to it, so I'm guessing that it isn't.
I'm very new to PHP, so my apologies if I'm just misunderstanding the documentation and/or script.
Steps to reproduce
Run codeunderneath Example #2Suggested fix
Here's my suggestion for how to make this example more helpful:
This has the same text output as the original and it still demonstrates that an evaluable expression can be used as the predicate of an if-then-else statement to conditionally skip HTML text. At the same time, it doesn't involve the unrelated semantics of comparing values to undefined variables.
Of course, this is by no means the only solution.