• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic PHP question

#1
What's the difference between these two below? Why would one be better than the other? I'm new so I new to PHP, so an easy-to-understand answer would be helpful.

PHP Code:
<?php
echo '<h2>My Site</h2>'
?>
verses just straight html

Code:
<h2>My Site</h2>
Reply

#2
If you only need HTML, just write HTML code. If you need to output the content of a variable or you need some logic, them use PHP to output your HTML code.

For example:
PHP Code:
<h1>My blog</h1>
<
h2><?= $title ?></h2>
<?php if ( ! empty($article)) : ?>
<p><?= $article ?></p>
<?php endif; ?>
Test your translation files with Translation Tester
Reply

#3
I have started using PHP to convert all HTML content into PHP strings.

PHP HereDoc is great for making the strings more readable: 

Do you think it is worth making the extra effort? I do because most of my time is spent debugging Sad

PHP Code:
<?php  
  declare
(strict_types=1);
 
 error_reporting(-1);
 
 ini_set('display_errors''1');
 
 ini_set('display_html_errors''1');

$title    'Title goes here';
$aArticle = [
 
 0,
 
 0.00,
 
 NULL,
 
 '',
 
 '   ',
 
 '&nbsp;'// asdf
 
 "",
 
 "   ",
 
 "&nbsp;",
 
 'this is the article'
];

// DEBUG 
 
 $DEBUG    '<pre>' .print_r($aArticleTRUE) .'</pre>';
 
 $rand     count($aArticle) -1;
 
 $iRand    mt_rand(0$rand ) ;

// ARTICLE
 
 $article  $aArticle[$iRand];
 
 $count    count($aArticle);
 
 $article  = empty($article) ? '' '<p>' .$article .'</p>';

// PHP HEREDOC string
 
 $tmp = <<< ____EOT
    <div style="height:8.8em">
      <h1> My blog </h1>
      <h2> 
$title  </h2>
      
$article
      <hr>
    </div>  
____EOT;
 
 echo $tmp;

// PHP HEREDOC string
 
 $DEBUG = <<< ____EOT
    <dl>
      <dt> Debug: </dt>
      <dd> Selection: 
$iRand </dd>
      <dd> 
$DEBUG </dd>
    </dl>
____EOT;
 
 echo $DEBUG
CI4 Develop Site - progressing nicely - Thank you Smile
https://johns-jokes.cf/pictures

Reply

#4
(08-07-2019, 12:12 AM)John_Betong Wrote: I have started using PHP to convert all HTML content into PHP strings.

PHP HereDoc is great for making the strings more readable: 

Do you think it is worth making the extra effort? I do because most of my time is spent debugging Sad

Nop, not worth it. You're creating a big headache for you in the future if your app grows.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply

#5
(08-07-2019, 12:12 AM)John_Betong Wrote: I have started using PHP to convert all HTML content into PHP strings.

PHP HereDoc is great for making the strings more readable: 

Do you think it is worth making the extra effort? I do because most of my time is spent debugging Sad

You're just making the computer work harder for nothing. Static HTML will always render faster.
Test your translation files with Translation Tester
Reply

#6
(08-06-2019, 05:22 PM)ronniebel Wrote: What's the difference between these two below? Why would one be better than the other? I'm new so I new to PHP, so an easy-to-understand answer would be helpful.

PHP Code:
<?php
echo '<h2>My Site</h2>'
?>
verses just straight html

Code:
<h2>My Site</h2>

Anything between the start and end processing instructions (the <?php and ?> tags) must be evaluated and converted into HTML (plain text) before being sent to the browser. So, the first example uses more server resources (memory, CPU cycles, etc) to get the same place as the second example.

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

#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 | john.betong@gmail.com">
    <link type="image/x-icon" href="/favicon.ico" rel="icon" />

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

return 
$result;

CI4 Develop Site - progressing nicely - Thank you Smile
https://johns-jokes.cf/pictures

Reply

#8
(08-08-2019, 10:27 AM)John_Betong Wrote: > @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.

I'd be curious to see benchmark and memory usage on this. I'm not convince your method performs better than plain HTML with some PHP variables but I guess it's a matter of personal preferences.

But I must say this is some weird code formatting! 

PHP Code:
PUBLIC function htmlHeader
(
 
 string $title PLATFORM .' - A wonderful book search utility'
 
 string $desc  'Yes we have no description???'
)
:
string // $result 
Test your translation files with Translation Tester
Reply

#9
(08-08-2019, 10: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

#10
Either one is fine. The performance difference is minimal. Use whichever works best for you and your needs.

That said, I find all of the PHP-based echos to make things much more difficult to scan through and read quickly. Whenever possible, I prefer to work in the language I'm actually using. If you're finding clarity lacking in your HTML code, then fix that: indent logically just as would your PHP code. Use HTML comments where necessary.

If you do use echo statements, please don't use the full version like you used in the example. There's a shorthand that is much cleaner for doing echoes:

Code:
<?= '<h2>My Site</h2>' ?>

Personally - better to use HTML and only use echoes where you need to output a variable. The person that reads your code after you will thank you for it.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.