addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscontroller-playcrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobe--smallglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1launch-new-window--smalllight-bulblinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonprintShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

PHP as a Social Activity Message Board Project Support › Code issue brought up at last Meet & Greet

Code issue brought up at last Meet & Greet

Aris
user 9423344
Tampa, FL
Post #: 23
We discussed this issue I was having with a very simple piece of code which apparently should produce the same results, but is not doing so for me. Thoughts?

The time is
<?php echo date ('H:i:s');?>
and the date is
<?php echo date ('j F Y');?>

vs.

<?php
echo "The time is ";
echo date('H:i:s');
echo " and the date is ";
echo date('j F Y');
?>
Sam L.
samuelelliot
Santa Ana, CA
Post #: 30
I ran this code and saw no error, and nearly exactly what I expected from both examples. Though the outputs aren't exactly the same, they are representing the correct output. The following is the output that I received:



The time is
04:44:20and the date is
6 March 2012
vs.

The time is 04:44:20 and the date is 6 March 2012




To make them match, you would need to modify one to resemble the other, as I'm not sure which snippet was the original, I'll modify them both to emulate the other.



The time is
<?php echo date ('H:i:s');?>
and the date is
<?php echo date ('j F Y');?>

vs.

<?php
echo "The time is\n";
echo date('H:i:s');
echo "and the date is\n";
echo date('j F Y');
?>




Providing the output:



The time is
05:00:15and the date is
6 March 2012
vs.

The time is
05:00:15and the date is
6 March 2012



The time is <?php
echo date ('H:i:s'); ?>
and the date is <?php
echo date ('j F Y');
?>


vs.

<?php
echo "The time is ";
echo date('H:i:s');
echo " and the date is ";
echo date('j F Y');
?>



Providing the output:



The time is 05:00:15 and the date is 6 March 2012
vs.

The time is 05:00:15 and the date is 6 March 2012




I assume that the latter is the desired output. The reason the difference occurs, is due to the PHP open and close tags being positioned with carriage returns before and after them. You can test this for yourself; if a PHP closed tag has a newline right after it, that newline is removed. Below I have added a single space character after the 2nd and 4th PHP close tags.



<?php echo ':'; ?>

<?php echo ':'; ?>

<?php echo ':'; ?>

<?php echo ':'; ?>

<?php echo ':'; ?>




Providing the output:



:
:

:
:

:




I believe this is a desired effect, as it thwarts a common issue with headers not being sent due to a newline at the end of PHP file after the PHP close tag, causing output to be sent to the browser before the headers had a chance to be sent. Remember that outside of the PHP tags the content is interpreted literally. If you want to provide the content in a readable manner without compromising the output, be cautious of all the characters outside of the PHP tags.



The time is <?php echo date ('H:i:s'); ?> and the date is <?php echo date ('j F Y'); ?>

The time is <?php echo date('H:i:s'); ?>
and the date is <?php echo date('j F Y'); ?>

The time is <?php echo date('H:i:s'); ?> and the date is <?php echo date('j F Y'); ?>

<?php echo 'The time is ', date('H:i:s'), ' and the date is ', date('j F Y'); ?>

<?php echo 'The time is '.date('H:i:s').' and the date is '.date('j F Y'); ?>




Providing the output:



The time is 05:00:15 and the date is 6 March 2012
The time is 05:00:15 and the date is 6 March 2012
The time is 05:00:15 and the date is 6 March 2012
The time is 05:00:15 and the date is 6 March 2012
The time is 05:00:15 and the date is 6 March 2012




I hope this clears things up.
Aris
user 9423344
Tampa, FL
Post #: 24
That was certainly a very detailed and helpful answer, but the problem I was having was that the second piece of code was not rendering any output on the screen. I am running MAMP (Mac) and could not produce results with the second code line. Wasn't sure why.
Sam L.
samuelelliot
Santa Ana, CA
Post #: 31
It sounds like you are experiencing an error which is halting the execution. This would cause the output to be seen up to the error, if error output is turned on you would see the error. It sounds like error output may be turned off, check your settings. You can also use the following to be certain error output is turned on.



<?php error_reporting(E_ERROR | E_WARNING | E_PARSE); ?>



This should force any errors after this point to display; for that reason, place this snippet at the top of your file.
Powered by mvnForum

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy