PHP
  Home arrow PHP arrow Page 5 - JV’s Power Tips for PHP (1)
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

JV’s Power Tips for PHP (1)
By: Justin Vincent
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 3
    2003-01-29

    Table of Contents:
  • JV’s Power Tips for PHP (1)
  • Never Breaking Out of PHP
  • Basic Template Theory
  • Refining the Previous
  • Accessing Other Variables from Within this Type of Template
  • Using Objects to Store Multiple Output Values
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    JV’s Power Tips for PHP (1) - Accessing Other Variables from Within this Type of Template


    (Page 5 of 7 )

    Of course not all PHP variables come in the form of $basic_var. So how can we access more complex variables such as associative arrays, objects, cookies and variables submitted by forms within our templates? Here’s how:

    Variables posted via a forms

    Simply wrap the standard variable access syntax within {}’s like so:

    <html>
      <head>
        <title>Simple Template</title>
      </head>
      <body>
        {$_POST[‘var_from_form’]}
      </body>
    </html>


    This works for associative arrays as well...actually this works anywhere in PHP. For example instead of this:

    <?php

      $turkey_speach = “Hello “ . $_POST[‘turkey_name’] . ” how are you?”;
      echo $turkey_speach;

    ?>


    You can do this and save yourself breaking the “’s:

    <?php

      $turkey_speach = “Hello {$_POST[‘turkey_name’]} how are you?”;
      echo $turkey_speach;

    ?>


    It also works here:

    <?php

    $turkey = array (“lover”=>”me”,”in_person” =>”dustin”);

    print “Page by {$turkey[‘lover’]}<p>”;

    print <<<END
      Hello {$turkey[‘in_person’]} how are you?<br>
      “OK?”
    END;

    ?>


    Object syntax works too:

    <html>
      <head>
        <title>Simple Template</title>
      </head>
      <body>
        $object_name->var_name
      </body>
    </html>


    You can also use object syntax within PHP print statements (and while assigning variables). I prefer using objects to arrays for this type of thing and often create objects on the fly to use in templates and dynamic output. Here’s how:

    <?php

    // Create an object on the fly..
    $output->{‘my_turkey’} = “Gobble”;
    $output ->{‘my_dog’} = “Rex”;

    $turkey_buffer = “Page by: $output->my_turkey:<p>”;

    echo $turkey_buffer;

    print <<<END
      Hello $output->my_dog how are you?<br>
      “OK?”
    END;

    ?>

    More PHP Articles
    More By Justin Vincent


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT