Home arrow PHP arrow Page 5 - JV’s Power Tips for PHP (1)
PHP

JV’s Power Tips for PHP (1)


In this article Justin provides some tips for improving our PHP applications such as a very simple but effective template technique.

Author Info:
By: Justin Vincent
Rating: 3 stars3 stars3 stars3 stars3 stars / 3
January 29, 2003
TABLE OF CONTENTS:
  1. · JV’s Power Tips for PHP (1)
  2. · Never Breaking Out of PHP
  3. · Basic Template Theory
  4. · Refining the Previous
  5. · Accessing Other Variables from Within this Type of Template
  6. · Using Objects to Store Multiple Output Values
  7. · Conclusion

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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;

?>

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- 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

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials