Welcome Guest, Not a member yet? Register   Sign In
Basic PHP question
#9

(08-08-2019, 09:27 AM)John_Betong Wrote: > @Dave friend 
> IMO, writing HTML as PHP strings is often much more difficult than dropping into and out of PHP mode. There isn't any performance hit for switching modes. On the other hand, creating a bunch of strings and then concatenating them with PHP variables can require quite a lot of CPU cycles. It can be a lot harder to write error-free code and like-wise results in code that is hard for a human to comprehend.

I think there must be a slight perfomance hit every time it is necessary to jump in, insert the variable then jump back out again. Far better just to prepare all the necessary variables then pass a single string. Passed variables can also be switched off by preceding each with a backward slash.

I used the phrase "dropping into and out of PHP mode" which is not an entirely accurate description of what is going on. I'd say "My bad" except even the PHP manual uses a variation on that phrase. It's not that the PHP processor is being turned off and on. Rather, it is that anything not contained within <?php ?> tags is ignored by the PHP compiler and passed directly to the web browser. So, "dropping out of PHP mode" is really only a matter of whether the script is sent to the parser or sent directly to the browser. So I must stick with the idea that there isn't any performance hit for switching modes.

Because all code inside of <?php ?> must be interpreted by PHP it will take longer to be passed to the browser. Admittedly, in many circumstances, we're looking at a micro-optimization here.

However, per the PHP manual (here)

PHP Manual Wrote:For outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo or print.

For me it's all about which one looks cleaner, is easier to write and to maintain.
Reply


Messages In This Thread
Basic PHP question - by ronniebel - 08-06-2019, 04:22 PM
RE: Basic PHP question - by includebeer - 08-06-2019, 05:14 PM
RE: Basic PHP question - by John_Betong - 08-06-2019, 11:12 PM
RE: Basic PHP question - by albertleao - 08-06-2019, 11:20 PM
RE: Basic PHP question - by includebeer - 08-07-2019, 02:57 AM
RE: Basic PHP question - by dave friend - 08-07-2019, 06:28 AM
RE: Basic PHP question - by John_Betong - 08-08-2019, 09:27 AM
RE: Basic PHP question - by includebeer - 08-08-2019, 10:12 AM
RE: Basic PHP question - by dave friend - 08-08-2019, 12:10 PM
RE: Basic PHP question - by kilishan - 08-08-2019, 02:27 PM



Theme © iAndrew 2016 - Forum software by © MyBB