1

I'm relatively new at PHP and came across a slight problem. I have a php page called info.php and use an included php file called components.php to pull functions that have html code in them that is then used in page.php (and other pages.) I put the title in a variable called $title and then reference that in my components.php, but for some reason the components.php doesn't recognize that as a title. Here's the code, and thanks for all help ( I know my description of the problem is hard. Let me know if you need any more info)

page.php

<?php 
   include("components.php"); 
   $title = "This is my page Title!";
   echo writeHeader();
?>

components.php

<?php
   function writeHeader()
   {
      echo <<<HED    
        <html>
         <head>
           <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
           <title>
      HED;

      echo $title;
      echo <<<HED

           </title>
      HED;
   }
?>
the_
  • 1,183
  • 2
  • 30
  • 61
  • 2
    maybe because it does not exists in the function, just pass it like: writeHeader($title); – Book Of Zeus Nov 03 '11 at 23:22
  • 1
    You should consider changing `writeHeader()` to *return* the string instead of `echo()`-ing it, then call `echo()` on what `writeHeader()` returns. – rid Nov 03 '11 at 23:33
  • Additionally(but unrelated to the actual question), I would recommend performing some kind of escaping to the $title before echoing it. Avoids unpleasant surprises(e.g. titles with '<') and potentially security issues(if the $title comes from the user input). Sorry if this seems unnecessary and off-topic, but I just figured someone ought to mention this just for the sake of completeness. – luiscubal Nov 03 '11 at 23:42
  • ... and for that, use [`htmlspecialchars()`](http://php.net/htmlspecialchars). – rid Nov 03 '11 at 23:44
  • thank everyone for helping... it worked perfectly... i upvoted all of the answers – the_ Nov 04 '11 at 00:41

3 Answers3

4

Good solution: Pass $title as a parameter to he function.

Less good solution: Declare $title as

   global $title

within the function to make it belong to the global scope.

Without declaring it as global, it is a fresh new variable.

To summarize the comments:

  • Never use global! Anything a function/method depends on, should be passed as a parameter.

  • Never access $_GLOBALS! Same reason.

  • Disable register_globals! No GET-parameters should be automatically injected into your application.

  • Enable the highest error level! Write your application in such a way, that no error or warning gets printed.

Unfortunately, my question regarding PHP newbie practices is closed. But it provides some helpful hints.

In case you like it, click the re-open link.

Community
  • 1
  • 1
SteAp
  • 11,853
  • 10
  • 53
  • 88
  • 1
    -1 Pass the variable to the function read this: http://stackoverflow.com/questions/5341131/php-performance-and-memory-issue-with-global-variables – Gino Sullivan Nov 03 '11 at 23:25
  • What's wrong with my answer? Why do I get a down vote? I proposed passing $title as parameter! – SteAp Nov 03 '11 at 23:27
  • I'll give your +1 back. Use of `global` is not deprecated and there is no good reason why your answer should be -1. http://php.net/manual/en/language.variables.scope.php – Patrick Moore Nov 03 '11 at 23:41
  • Thank you! While I don't like global and I never use it, it might be helpful for a newbie to just know that it exists. – SteAp Nov 03 '11 at 23:43
  • @Radu Well, yes. Or no. Hm, I prefer to know each aspect or a new language or framework - and never use. But I see your point. I wonder anyway, if PHP is the best language to get started with web-programming. I'd propose a stricter language. – SteAp Nov 03 '11 at 23:50
4

A variable has a scope. When you declare it, unless it's global you can't use it in another scope. To do that you have to pass it to the other scope using parameters

Your code should be like that :

page.php

<?php 
   include("components.php"); 
   $title = "This is my page Title!";
   echo writeHeader($title);
?>

components.php

<?php
   function writeHeader($title)
   {
      echo <<<HED    
        <html>
         <head>
           <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
           <title>
      HED;

      echo $title;
      echo <<<HED

           </title>
      HED;
   }
?>
Nicolas Thery
  • 2,319
  • 4
  • 26
  • 36
1

In your code example, you call the echo function at two places :

  1. In your page.php file : This one does nothing
  2. In your component.php : This one prints the text

The best practice is to group the prints into a single component called the "view". If your view is the file called component.php then you should remove the first echo.

Nicolas Thery
  • 2,319
  • 4
  • 26
  • 36
  • 1
    If you are beginner with PHP, I advise you to directly start using a good architecture. A good framework called CODEIGNITER gives you the Model View Controller Pattern – Nicolas Thery Nov 03 '11 at 23:41