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

> @albertleao 
> Nop, not worth it. You're creating a big headache for you in the future if your app grows.

Actually as mentioned I find using this technique makes the script far simpler to read and understand. I will definitely continue using as you may notice from the following example it is very easy to swap, change and test different variables.


> @includebeer 
> You're just making the computer work harder for nothing. Static HTML will always render faster.

The PHP script is a single block and I think vastly reduces processing because it eliminates the need to jump in and out of HTML script to insert variables.


> @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.

Here is another example which hopefully explains a little better. Usage in the main index.php file is very clean and eliminates loading a file: 

Give it a try and I am sure you will be able to see the benefits.

PHP Code:
//======================================================
PUBLIC function htmlHeader
(
 
 string $title PLATFORM .' - A wonderful book search utility'
 
 string $desc  'Yes we have no description???'
)
:
string // $result
{
 
 $result '';

 
 $www  '//www.'.PLATFORM ;

 
 $tmp  '/assets/css/style.css';
 
 $css  '<style>' 
 
         file_get_contents(DOC_ROOT .$tmp
 
       '</style>';

// CRUNCH        
 
 $css  str_replace(["\n""\t""  "], ''$css);      
          
  $result   
= <<< ____TMP
    <!DOCTYPE HTML>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta name="viewport"     content="width=device-width, initial-scale=1.0">
    <title> 
$title </title>
    <meta name="description"  content="
$desc">
    <meta name="author"       content="John_Betong | [email protected]">
    <link type="image/x-icon" href="/favicon.ico" rel="icon" />

    
$css
    
    </head>
    <body>
____TMP;
 
 $result .= $this->_logo($title);

return 
$result;

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