You are here:

PHP5/Understanding a bit of code

Advertisement


Question
I'm still a super-newbie at PHP.  I don't even know the basic syntax yet, beyond the <?php   ; ?>
I'd like to understand each of these line-by-line.

What is the .= operator?  Is it like += ?
So is it concatenating the full path onto the beginning of what you consider the "full" relative path (the "/common/header.php") and include that into the page?  So each separate include would be another copy/paste of the same three lines of code?

What's the difference between include() and include_once()?

In the second example: this would avoid code duplication to some extent?  Put at the top of, say, /somedir/somepage.php and then just <?php include("/blahblah/otherpage.php"); ?> and it would work?
Can the second example be in a config file, and set the include path for everything?  I don't know?  If so, what config file and where and how?

_______________________________________________
<?php
  $path = $_SERVER['DOCUMENT_ROOT'];
  $path .= "/common/header.php";
  include_once($path);
?>
_______________________________________________

or this one:
_______________________________________________
<?php set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] ); ?>
_______________________________________________

Both from http://css-tricks.com/php-include-from-root/

Would it be better to use $_SERVER['HTTP_HOST'] in these examples?  My purpose is exactly that of the link/url.

PS - if AllExperts can't handle PHP code pasted in here, not my fault :)

Answer
Hello Karen,
         Hope you are doing great. My apologies for the long delay in getting back to you.


What is .= operator ?

Assume the Example, you want to add the fruit names to a variable called "fruits". Then, .= operator can be used as follows.

<?php
$fruits="apple";
$fruits.="orange";
$fruits.="pineapple";
?>

If you print the $fruits variable, the following would be printed.

appleorangepineapple




What is the Difference Between include() and include_once() ?

The include function includes the file, irrespective of checking whether the file has already been included or not. Whereas, include_once checks for any other instance of the file. If already opened, it ignores and does'nt include the file. If not, it includes it.





I AM AFRAID, I CAN'T BE ABLE TO UNDERSTAND THE FOLLOWING FROM YOUR QUESTION. COULD YOU BE MORE SPECIFIC PLEASE ?


In the second example: this would avoid code duplication to some extent?  Put at the top of, say, /somedir/somepage.php and then just <?php include("/blahblah/otherpage.php"); ?> and it would work?
Can the second example be in a config file, and set the include path for everything?  I don't know?  If so, what config file and where and how?

_______________________________________________
<?php
 $path = $_SERVER['DOCUMENT_ROOT'];
 $path .= "/common/header.php";
 include_once($path);
?>
_______________________________________________

or this one:
_______________________________________________
<?php set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] ); ?>
_______________________________________________

Both from http://css-tricks.com/php-include-from-root/

Would it be better to use $_SERVER['HTTP_HOST'] in these examples?  My purpose is exactly that of the link/url.




Thank You.  

PHP5

All Answers


Answers by Expert:


Ask Experts

Volunteer


Vimal Abraham

Expertise

I can be able to solve PHP, MySQL and Web Hosting (Linux) related issues that may include cPanel/WHM.

Experience

I have worked under various domains of web development including file sharing, social community, wallpaper management, image hosting, web hosting automation, shopping cart and payment gateway integration. My active portfolios include, http://www.4kwallpapers.in http://www.wallpicz.com

Education/Credentials
I have completed Bachelor of Engineering in Computer Science.

Awards and Honors
Have a Total of nine certificates in Inter and Intra College Competitions that includes technical quiz, program debugging and paper presentation (State Level).

©2016 About.com. All rights reserved.