PHP vs HTML parsing

GSR_WildCard

Member
Joined
Sep 27, 2007
Messages
450
Reaction score
0
Heyos, all.

Have a very weird thought I need answered.

Look at the following code snippets:

<?php
require('connect.php');
$people = mysqli_query($con , "SELECT * FROM people WHERE people_id = '".$pplid."'");
$people_attrib = mysqli_fetch_array($people); ?>
<div class='peopleDisplay'>
<?php echo "<img src=http://graph.facebook.com/".$peopleID."/picture?type=large>";?>
<br>
<?php echo $people_attrib['first_name'] . ' ' . $people_attrib['last_name']; ?>
<br>
<?php echo $people_attrib['gender']; ?>
<br>
<?php
if (($people_attrib['location'] == NULL) || ($people_attrib['location'] == ''))
{
echo "Unknown location";
}
else
{
echo $people_attrib['location'];
}
?>
</div>


<?php
require('connect.php');
$people = mysqli_query($con , "SELECT * FROM people WHERE people_id = '".$pplid."'");
$people_attrib = mysqli_fetch_array($people);
echo "<div class='peopleDisplay'>";
echo "<img src=http://graph.facebook.com/".$peopleID."/picture?type=large>";
echo "<br>";
echo $people_attrib['first_name'] . ' ' . $people_attrib['last_name'];
echo "<br>";
echo $people_attrib['gender'];
echo "<br>";
if (($people_attrib['location'] == NULL) || ($people_attrib['location'] == ''))
{
echo "Unknown location";
}
else
{
echo $people_attrib['location'];
}
echo "</div>";

One uses multiple <?php tags while using HTML code for the rest while the other encapsulates the whole section under <?php and uses echo for HTML statements.

File is saved as .php
In theory, which has higher server performance overhead?

N.B. Pls excuse if the code is not up to industry standards. Just started learning web development as part of my course.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
Heyos, all.

Have a very weird thought I need answered.

Look at the following code snippets:






One uses multiple <?php tags while using HTML code for the rest while the other encapsulates the whole section under <?php and uses echo for HTML statements.

File is saved as .php
In theory, which has higher server performance overhead?

N.B. Pls excuse if the code is not up to industry standards. Just started learning web development as part of my course.

Generally as long as the code is to be handled by PHP, it is not the most significant overhead when coming to parsing PHP. Your bottleneck often happens elsewhere unless it is a simple HelloWorld application.

I don't dwell much into those aspect between fully writing an application using PHP codes or having PHP codes embedded among HTML codes; reason is simple. Both approaches are not the most advocated approach to web development regardless of programming languages. You are always encouraged to use various MVC approaches in recent web development methodology to segregate as much of logic away from the view/presentation layer.

I would suspect internally the HTML codes between PHP tags are slurp in and response as-is with no interpretation. This is probably faster since strings within 'echo' or 'print' statements are still liable to variables interpolation unless you are using single quotes. Even so, multiple statement calls to the functions are probably not going to be faster unless PHP has optimisation that combine several occurrences of print or echo together internally.

For PHP, all requests will normally result in tokenisation, parsing and compilation unless you cache using components like APC, XCache or any other such opcodes caching solution.

Moving into real industrial deployment, you will find caching happens at other levels too, using solutions like Varnish, CDN, or even browser caching of HTML results. These approaches will allow better scaling instead of the above optimisation that you have look at. If you are interested, look at the HVVM solution from Facebook to compile PHP codes into C for deployment. HVVM has its own set of caveats and will not serve all possible rendition of PHP codes.

My general advice to you is to not dwell too much into these as part of your learning. It's good to know, but you will realise industrial approaches nowadays are not optimising on everything, but things that make sense and have significant impact. There is the balance to maintain between cost-effort-time, and performance. The 0.1% performance you get out from this exercise will not scale for a web application meant for 1 million users. That could be easily upgraded from a 2Ghz machine to 2.3Ghz. Maintenance of the code, readability, consistency and design patterns for large scale development plays very important role to the success of a project from a long term perspective. There is absolutely no good reason to get 10% output performance if, at the end of the day, your company need to spend another 20% cost to upgrade, migrate or have knowledge transfer between developers.

That's my 2 cents experience for you to consider.
 
Last edited:

GSR_WildCard

Member
Joined
Sep 27, 2007
Messages
450
Reaction score
0
Generally as long as the code is to be handled by PHP, it is not the most significant overhead when coming to parsing PHP. Your bottleneck often happens elsewhere unless it is a simple HelloWorld application.

I don't dwell much into those aspect between fully writing an application using PHP codes or having PHP codes embedded among HTML codes, reason is simple. Both approach is not the advocated approach to web development regardless of languages. You can always encouraged to use MVC approach in recent web development methodology to segregate as much of logic away from the view/presentation layer.

I would suspect internally the HTML codes between PHP tags are slurp in and response as-is with no interpretation. This is probably faster since strings within 'echo' or 'print' statements are still liable to variables interpolation unless you are using single quotes. Even so, multiple statement calls to the functions are probably not going to be faster unless PHP has optimisation that combine several occurrences of print or echo together internally.

For PHP, which request will normally result in the whole tokenisation, parsing and compilation unless you cache using components like APC, XCache or any other such opcodes caching solution.

Moving into real industrial deployment, you will find caching happens at other levels too, using solutions like Varnish, CDN, or even browser caching of HTML results. This approaches will allow better scaling instead of the above optimisation that you have look at. If you are interested, look at the HVVM solution from Facebook to compile PHP codes into C for deployment. HVVM has its own set of caveats and will not serve all possible rendition of PHP codes.

My general advice to you is not dwell too much into these as part of your learning. It's good to know, but you are realise industrial approaches nowadays are not optimising on everything, but things that make sense and have significant impact. There is balance to maintain between cost-effort-time, and performance. The 0.1% performance you get out from this exercise will not scale a web application for 1 million users. That could be easily upgrade from a 2Ghz machine to 2.3Ghz. Maintenance of the code, readability, consistency and design pattern for large scale development plays very important role to the success of a project from a long time perspective. There is absolutely no good reason to get 10% output performance if at the end your company need to spend another 20% cost to upgrade, migrate or knowledge transfer between developers.

That's my 2 cents experience for you to consider.

Thanks for the reply.
Just read up in concept of MVC and playing with it.
My field is more on infrastructure and, yes, we do deploy frontend caching or web acceleration techniques for various of our projects / clients, Varnish being the most popular as its simple and just works.
But those are quite big projects and this one I'm working on for my degree course, I could commercialize it if I can roll it out within my planned deadline. Just that have doubts if I can afford such a scale.
But I get your point on code readability and maintainability.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
Thanks for the reply.
Just read up in concept of MVC and playing with it.
My field is more on infrastructure and, yes, we do deploy frontend caching or web acceleration techniques for various of our projects / clients, Varnish being the most popular as its simple and just works.
But those are quite big projects and this one I'm working on for my degree course, I could commercialize it if I can roll it out within my planned deadline. Just that have doubts if I can afford such a scale.
But I get your point on code readability and maintainability.

For startups, cloud computing is your most viable infrastructure to get your concept into realisation within the least timeframe and least investment. My recommendation is go find out more about AWS. It has one of the most elaborate infrastructure for you to architect sizable project that can capture large audiences should your idea make it onto the expressway.

You will need to remodel the way you think about project development, deployment and go truly cloud with the idea of "volatiality" very seriously if you want to cut cost using AWS. There are just too many parties going in with a very narrow mindset of how to enjoy the benefits of cloud computing. Cloud computing doesn't go small, they go big and it's about the approach you operate in it where you get saving. It's not easy to calculate but you will feel it as you use it properly along the way.

Just some technologies they offer to allow you architect your solution. EC2(on-demand, reserved, spot) instances to suit different needs of computing power ownership. Extremely reliable EBS to serve iSCSI like storage solution for your computing nodes. S3, an extremely scalable and durable object base storage that can also serve as backup storage. Cloudwatch, your unclose monitoring infrastructure to allow you monitor varies aspect of the services. ELB, the essential load balancing feature in AWS that allow you to distribute load between your computing nodes. Cloudfront, AWS own brew of content distribution that allow origin from EC2 and also S3. Route53, AWS DNS solution that couples with other technologies to give you extremely powerful global redundancies using health check and various features in it. Opsworks, your operational tool to allow Chef-like deployment mechanism in AWS. RDS, a database service of Oracle DB, MySQL DB, SQL Server that allow you to offload database management like upgrade, availability to AWS instead of you handling it. SimpleDB, DynamoDB, NoSQL offering from AWS. Cloudformation, the automated provisioning helper for your computing nodes. Autoscaling, the feature and allow truly cloud volatility based on usage pattern of your solution.

What I have least scratches the surface of AWS. It is pointless to just know all these. It takes awhile to understand the usage of these services and their caveats. I believe you will want to start learning about these if you want to build robust and dynamic solution on AWS.
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top