Mixed HTML and PHP in Echo

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Hey hey,

Can someone show me how I need to structure this piece of code of mine to be used as an echo?

I tried escaping the double quotes, but that just ended up echoing the php as a string ie the literal text instead of allowing it to execute.

To give some background, I want to place this inside a for each loop.

PHP:
 <div class="stock <?php if ($percentage >= 0) {echo 'green';} else { echo 'red';} ?>" id="stock-1">
  <span class="symbol"><?php echo $company; ?></span>
  <span class="change"><?php if ($percentage >= 0) {echo '+';} else { echo '';} ?><?php echo $percentage; ?>%</span>
</div>

Help is greatly appreciated.
 

Nod

Honorary Master
Joined
Jul 22, 2005
Messages
10,057
I changed everything into a php echo:
Code:
#!/usr/bin/php
<?php
error_reporting(E_ERROR | E_PARSE);
$company = "PnP";
$percentage = "10";
echo "<div class=\"stock"; if ($percentage >= 0) {echo 'green';} else { echo 'red';} echo "\" id=\"stock-1\">
<span class=\"symbol\">$company</span>
<span class=\"change\">";if ($percentage >= 0) {echo '+';} else { echo '';} ;
echo $percentage;echo "%</span>"; 
?>

No you can easily put it into a function for the DIV:
Code:
#!/usr/bin/php
<?php
error_reporting(E_ERROR | E_PARSE);
$company = "PnP";
$percentage = "10";
function doCompDiv($company, $percentage) {
echo "<div class=\"stock"; if ($percentage >= 0) {echo 'green';} else { echo 'red';} echo "\" id=\"stock-1\">
<span class=\"symbol\">$company</span>
<span class=\"change\">";if ($percentage >= 0) {echo '+';} else { echo '';} ;
echo $percentage;echo "%</span>\n"; 
}

doCompDiv($company, $percentage);
doCompDiv("Checkers", 5);
?>

This produce:
Code:
~$ ./t.php
<div class="stockgreen" id="stock-1">
<span class="symbol">PnP</span>
<span class="change">+10%</span>
<div class="stockgreen" id="stock-1">
<span class="symbol">Checkers</span>
<span class="change">+5%</span>
 

_kabal_

Executive Member
Joined
Oct 24, 2005
Messages
5,923
also, if you MUST echo php directly in html, stop using echo, and use the built in language constructs

PHP:
<?php if ($percentage >= 0) {echo 'green';} else { echo 'red';} ?>

vs

PHP:
<?= $percentage >= 0 ? 'green' : 'red' ?>
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
also, if you MUST echo php directly in html, stop using echo, and use the built in language constructs

PHP:
<?php if ($percentage >= 0) {echo 'green';} else { echo 'red';} ?>

vs

PHP:
<?= $percentage >= 0 ? 'green' : 'red' ?>

Pros and cons?

I used echo for two reasons:

I'm a noob did not know better

And

It reads easier.
 

_kabal_

Executive Member
Joined
Oct 24, 2005
Messages
5,923
If you are using php 5.3 or less you need to enable short tags to use this syntax.

I think that if you are inlining php tags, effectively using it as a templating language, then using the ternary operator and php's built in template tags is far superior and readable compared to littering your templates with long php tags, if/else and echoes.
 
Last edited:

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
If you are using php 5.3 or less you need to enable short tags to use this syntax.

I think that if you are inlining php tags, effectively using it as a templating language, then using the ternary operator and php's built in template tags is far superior and readable compared to littering your templates with long php tags, if/else and echoes.

Is there a performance gain?

For me at this stage given my noobly Ness I prefer readability however if it's a performance concern then I'll change my habits immediately.
 

_kabal_

Executive Member
Joined
Oct 24, 2005
Messages
5,923
You keep referring to less readable code as more readable????

Just because you are used to something doesn't mean it is now more readable. Maybe to you in the short term.

How is

PHP:
<?php if $isFoo {echo 'foo';} else { echo 'bar';} ?>

More readable than

PHP:
<?= $isFoo ? 'foo' : 'bar' ?>

Performance is also pretty much always irrelevant in these situations. Your network adds more latency than your code.
 
Last edited:

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
You keep referring to less readable code as more readable????

Just because you are used to something doesn't mean it is now more readable. Maybe to you in the short term.

How is

PHP:
<?php if $isFoo {echo 'foo';} else { echo 'bar';} ?>

More readable than

PHP:
<?= $isFoo ? 'foo' : 'bar' ?>

Performance is also pretty much always irrelevant in these situations. Your network adds more latency than your code.

So no performace concern?
 

_kabal_

Executive Member
Joined
Oct 24, 2005
Messages
5,923
Why are you worrying about performance? You have no ability to optimize for performance. Learn to do a for loop first.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Why are you worrying about performance? You have no ability to optimize for performance. Learn to do a for loop first.

Because if there is no performance issue then I'm moving on as you are trying to form a <br> vs <br /> vs <br/> argument it's irrelevant to me unless what I a doing is wrong in that that it affects the overall program.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Also that for loop comment of yours came across a bit snarly and uncalled for so moving on.
 

_kabal_

Executive Member
Joined
Oct 24, 2005
Messages
5,923
It is quite clear that writing human readable, clean and maintainable code is irrelevant to you.

And btw, if you are writing an valid XHTML document, then <br /> is the only valid way of doing it. Thankfully HTML5 does not give a crap and <br> is fine and dandy
 
Last edited:

biometrics

Honorary Master
Joined
Aug 7, 2003
Messages
71,858
Thor, you're not really at the performance code level yet, rather learn how to get stuff to work. You can then optimize sections of code like downloading and storing the html in a db and serving and updating it in a timed fashion. You can then change it to a cron job.

Often it's 10% of the code taking 90% of the time, optimizing that has the greatest effect for the least effort.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
It is quite clear that writing human readable, clean and maintainable code is irrelevant to you.

And btw, if you are writing an valid XHTML document, then is the only valid way of doing it. Thankfully HTML5 does not give a crap and is fine and dandy

No I think we are missing each other.

For me, writing it out else is more readable than having a bunch of shirt codes all over the place.

The point I was making is, this seems to come down to preference as there is no performance impact.

What is mean by that is that I prefer to write the code which is mine and mine alone by using the "long" method as its what I know and it is what I understand and like quite frankly to me it looks better than a short code.

And I was saying I would be willing to have change my habit immediately to use short codes if my way of doing impacted my applications negatively which we have now determined it does not.

So I will not use short codes for the time being.

I don't see why that is wrong or a issue?
 
Last edited:

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Thor, you're not really at the performance code level yet, rather learn how to get stuff to work. You can then optimize sections of code like downloading and storing the html in a db and serving and updating it in a timed fashion. You can then change it to a cron job.

Often it's 10% of the code taking 90% of the time, optimizing that has the greatest effect for the least effort.

Fully agree and understand, hence why I don't see the need to switch to short codes now that will just confuse me in my current level of understanding.
 

Other Pineapple Smurf

Honorary Master
Joined
Jun 21, 2008
Messages
14,593
I'm personally not a fan of shortcodes. I've got to maintain a lot of legacy PHP code and the shortcodes become hard to read in larger files.

Then again I've been doing PHP for over 10 years.
 
Top