23.12.2012 Posted November 21, 2010 Share Posted November 21, 2010 I've been teaching myself C for quite some time and now I'm trying to write a new PHP application, just to catch up a little bit. So I was thinking of actually creating a PHP file containing all the function declarations and definitions and including it in the header.php file, like I'm doing below. Would this be a good idea, or would there be a better way to go about it? Thanks in advance!libirr.php function irr_title_display();function irr_header_display();/* Some other prototypes */function irr_title_display() { echo 'Title';}function irr_header_display() { echo 'Header';}/* The rest of the definitions */ header.php <?php require_once "libirr.php" ?><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <head> <title><?php irr_title_display(); ?></title> </head> <body> <div id="header"> <div id="title"> <h1><?php irr_header_display(); ?></h1> <h4><?php irr_subheader_display(); ?></h4> </div> <div id="login"> <p>Welcome, <strong><?php irr_user_display(); ?></strong>!</p> </div> <div id="nav"> <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> <li>Item 5</li> </ul> </div> </div> Link to comment Share on other sites More sharing options...
jeffman Posted November 21, 2010 Share Posted November 21, 2010 It is certainly possible to get where you want to go.Note that PHP, unlike C, does not require functions to be prototyped before they are defined. You can define a function anywhere you want to.An included file can also contain variable definitions. For example, if the return value of irr_title_display() is always the same (not dynamic) it would be more efficient to define it as a variable, and then your title tag would look something like this:<?php echo $title; ?>That assumes that header.php was included into the global space (not inside a function). Link to comment Share on other sites More sharing options...
boen_robot Posted November 21, 2010 Share Posted November 21, 2010 A file that contains all functions is fine, but do one that contains the function implementations, not just prototypes.The reason prototypes exist as separate files in C/C++ is to let a compiled library be embedded into a non compiled code, but there's no such thing in PHP, so you only need the actual implementations. Link to comment Share on other sites More sharing options...
23.12.2012 Posted November 21, 2010 Author Share Posted November 21, 2010 They are dynamically generated, I have just written a fast example in the code above. So would this be a good practice? Link to comment Share on other sites More sharing options...
jeffman Posted November 21, 2010 Share Posted November 21, 2010 Sure, and very common. Essentially what include/require are designed for, which in this case is a lot like C. (They can do a few other things also.) Link to comment Share on other sites More sharing options...
23.12.2012 Posted November 21, 2010 Author Share Posted November 21, 2010 Thanks a lot! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.