I am working on a PHP project. There, I often use following syntax to output text in a cluase:
if($boolean){
?>
output text
<?
}else{
?>
alternative
<?
}
On my computer, this works perfectly well. I use XAMPP foer Mac OS X. But when I send the files to my coworker, these outputs often do not work and the compiler complains about having reached an unexpected $end of file. This occurs especially often when there is a tag in the output. We have to replace the means of output with echo.
What's the reason for this strange behavior of the compiler? Is the above-mention syntax of outputting text wrong?
Use <?php instead of <? he might not have short tags enabled.
I think you'll find this topic useful;
Are PHP short tags acceptable to use?
Related
Ok, someone has just shown me a piece of PHP code and at the end of the file I've seen a stray <?php } ?> . I thought that should give a compilation error, but it doesn't.
Why is:
<?php
if(1==1){
?>
X
<?php } ?>
valid?
Is it safe to split a statement into multiple php blocks?
PS: I was expecting for something more from the answers then "yes" :D
Yes that is fine, but I would suggest:
<?php if(1==1):?>
X
<?php endif; ?>
It makes it a little more readable then random { and }
From the manual:
Everything outside of a pair of opening and closing tags is ignored by
the PHP parser which allows PHP files to have mixed content. This
allows PHP to be embedded in HTML documents, for example to create
templates.
Welcome to the mysterious world of PHP.
Safe? Yes.
Readable? Not really.
Avoid mixing your PHP logic with your HTML where possible. There are few times when this is a good idea, as it makes reading through and understanding your code difficult.
Yes, this is fine.
It's often useful to drop out of "php mode" for large blocks of HTML - you'll see this technique used anywhere HTML and PHP are mixed.
It is valid, but not recommended if you want to have a code that is maintainable and readable in the long run.
You must bear in mind that every time you "exit" from PHP, you are entering HTML.
I run PHP in JavaScript files, e.g....
var = '<?php /*some code*/ ?>';).
I need to use JavaScript to scan a string for PHP delimiters (the < ? php and ? > that open and close PHP).
I already know the code using JavaScript...
if (b.value.indexOf('<?php')>-1) {alert('PHP delimiter found.');}
What I'm having trouble with is that I need to keep the ability for PHP to be interpretted in JavaScript files (no exceptions). I simply need to output the delimiter strings to the client in JavaScript and not have them interpreted by the server.
So the final output (from the client's view) would be...
if (b.value.indexOf('<?php')>-1) {alert('PHP delimiter found.');}
With the following code...
if (b.value.indexOf('<?php echo '<?php'; ?>')>-1 || b.value.indexOf('<?php echo '?>'; ?>')>-1)
I get the error: "Parse error: syntax error, unexpected T_LOGICAL_AND"
Javascript will never find the <?php in your strings because simply, they have already been parsed by your PHP Server. Javascript is a client-side script and is executed after your server-side scripts.
You could take advantage of Javascript's ability to parse hex as a character:
if (b.value.indexOf('<\x3fphp')>-1) {alert('PHP delimiter found.');}
In Javascript '<\x3fphp' is exactly the same thing as '<?php', but it has no meaning in PHP.
Use output buffering on the PHP, then htmlspecialchars() on the buffered output. You then search for the HTML entities with the JavaScript.
the first thing that came into my mind and should work is this simple, little "cheat":
<?php echo '<'.'?php'; ?>
the php interpreter doesn't see a <?php, but the output is as desired.
<?php echo '<?php'; ?> ... <?php echo '?>'; ?>
In my vim files, when I have a closing php tag in a string, my syntax highlighting stops at that point
Example:
<?php
... Everything good up to this point
$xml = '<?xml version="1.0"?>';
$foo = 'bar' <-- starting here I loose highlighting to the end of the file
?>
I'm running version 7.2.245. anyone have ideas why this happens? is it only me? or is this a known bug?
Make sure you have the latest version of the runtime files, or at least the very latest version of the php syntax file.
Try using this alternative syntax file instead.
It isnt a bug, you have actually ended the PHP string. you need to just break it up. Do like "'.'?'.'>' or something
This is normal. Notice the SO syntax highlighter stopped highlighting at that point too ;)
My windows editor does the same thing.
Even PHP's built in function works this way (or used to for a long time)
While working on an existing Drupal site I've noticed a strange glitch that causes any PHP operator with > in it to act like a ?> tag. This is happening in in Drupal pages that I create that have a 'Input Format' of 'PHP code'.
For example this line of code
foreach($array as $key => $value){
results in a very broken page that prints out
$value){
Does any one know what could be causing this? My Dev environment is XAMPP. Drupal version is 6.15. PHP version is 5.2.9.
UPDATE: Short tags are OFF and when the PHP is rewritten so that it contains no > char it works as expected. I'll have to test more to get additional information.
Check if your php.ini has short tags enabled. If not, make sure you always begin every php block with <?php
If you look in the source, I bet you see the rest of the foreach in there. It's treating > as an HTML end delimiter (and it found a < earlier in the script). Ensure PHP is being parsed. If it isn't being parsed, see if it's because the script requires short tags. If the script uses long tags, make sure PHP itself is up and running properly in the web server.
are you sure there is <?php infront of the foreach()? some server need the <?php and not just <?
I recently upgraded to xampp v1.7.2 which dumped PHP 5.3 on me. Along with that all my httpd.confs and php.ini's got wiped out (I had taken it for granted that this release will be an upgrade like all the earlier xampp releases). Anyway, it took me a while to reconfigure all the services - but now I've run into a funny problem.
This self-written CMS that is used in my workplace uses a lot of the alternative conditional syntax for if-else, i.e.
if( condition ): ?>
<some html />
<?php else: ?>
<some other html />
<?php endif;
This used to work fine with PHP 5.2.x that came along with xampp 1.7.1 - and now suddenly such code blocks are producing this error:
Parse error: syntax error, unexpected
T_ELSE in ...
I haven't altered my script in any way - the same used to work absolutely without a hitch in PHP 5.2.x.
So my question is, does PHP 5.3 allow such alternative conditionals? Or do I have to turn on some hidden option in my config files?
Please note that I don't use shorttags - so they aren't an issue here.
Although the if/else syntax hasn't changed in 5.3, many other parts of the syntax have. You should check the lines just before the else statement in question to see if one of the other new syntax features is confusing the parser.
If you can't figure out where the problem is, you can always just start systematically deleting lines of code until you're left with the following three lines:
<?php if(condition): ?>
<?php else: ?>
<?php endif ?>
Update: You really should test your code with short_open_tag turned on, because the syntax error you see is what you would get if you had this code somewhere:
<? if(condition): ?>
<?php else: ?>
<?php endif ?>
Yes, PHP 5.3 allows for alternative syntax for control structures, including your conditional statements.
I would suggest trying to debug by replacing the alternative syntax with regular syntax in one or two places to see if it fixes the problem. If it does, then you know for sure what the issue is.