doubledee Posted April 23, 2011 Share Posted April 23, 2011 Some questions about PHP Constants... 1.) What make a Constant "come to life"? 2.) What is the life of a Constant? 3.) Can you define a Constant in "file_1.php" and use the Constant in "file_2.php" and "file_3.php"?? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/ Share on other sites More sharing options...
Pikachu2000 Posted April 23, 2011 Share Posted April 23, 2011 1) Huh? 2) The duration of the script. 3) Only if you pass the value, or have it defined in an included file, etc. Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205116 Share on other sites More sharing options...
doubledee Posted April 23, 2011 Author Share Posted April 23, 2011 1) Huh? 2) The duration of the script. 3) Only if you pass the value, or have it defined in an included file, etc. So there is no way to define a Constant and have it live for the life of an entire application or for all time? (I was hoping to define some file paths and use them in all scripts. For example where the Web Root is.) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205117 Share on other sites More sharing options...
Pikachu2000 Posted April 23, 2011 Share Posted April 23, 2011 include()'ing a config. file in which the constant is defined does pretty much exactly that. What are you trying to accomplish? Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205120 Share on other sites More sharing options...
doubledee Posted April 23, 2011 Author Share Posted April 23, 2011 include()'ing a config. file in which the constant is defined does pretty much exactly that. What are you trying to accomplish? *evil laugh* Okay, Pikachu2000, I have gotten my butt royally kicked today and am still confused about several things... (Posted earlier, but no one replied, so I pray you can help out... Before I type a bunch, are you willing to hear me out and try to help now? Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205123 Share on other sites More sharing options...
Pikachu2000 Posted April 23, 2011 Share Posted April 23, 2011 I'm fixing to go to sleep, but I can look at it again when I wake up if I can't get to it tonight. Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205126 Share on other sites More sharing options...
doubledee Posted April 23, 2011 Author Share Posted April 23, 2011 I'm fixing to go to sleep, but I can look at it again when I wake up if I can't get to it tonight. If you can hang on for a few minutes now, I'll try to get to the point ASAP. (I'm desperate after 14 hours of failure...) *pretty please* Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205128 Share on other sites More sharing options...
Pikachu2000 Posted April 23, 2011 Share Posted April 23, 2011 I'll give it a shot, but can't promise anything sight-unseen like that . . . Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205130 Share on other sites More sharing options...
doubledee Posted April 23, 2011 Author Share Posted April 23, 2011 I had a simple website almost done. Looked really sharp. Problem was 80% of each page was the same, and I'm a CSS-tweaking-maniac. So I split up my home page into like 8 parts yesterday. Now index.php includes... header.php left_col.php right_col.php footer.php and so on... page_2.php includes most of the same pages. All fine and good. But now the problems... (Hope I get this right, because I'm so confused, I'm confused about what I'm confused about?!) So I have a directory similar to this... 00_MyWebsite Source Files components body_header.inc.php body_footer.inc.php secure checkout.php images my_logo.png index.php product_details.php business smallbiz article.php The problem I discovered this morning was that when I started including my Header from different locations things blew up! If I include "body_header.inc.php" from "index.php" all is well. But if I include "body_header.inc.php" from "checkout.php" it not only requires a different include path but it insanely messes up the HTML reference to "images/my_logo.png" If I changed the HTML path to the logo for one page it broke it for the next. (I *think* using an Absolute HTML Path might fix this?!) Lastly, there is the issue that if I duct-tape this together and get it working locally from my "00_MyWebsite" project folder in the web root of NetBeans, it will break when I upload things to my Web Host... Lots of issues, but I think I might be over-thinking things?! I need expert help!! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205132 Share on other sites More sharing options...
gizmola Posted April 23, 2011 Share Posted April 23, 2011 Hey Debbie, PHP has page scope. People either love that or hate it, but it's a basic principle of the language in web application use. Variables exist for the lifetime of the script and not beyond. I think I probably alluded to this a while back, in a reply to one of your earlier posts, but this is why it is good to have a controller script that is the basis of your site, and through which everything runs, rather than having many individual independent scripts. This allows your controller script to include things that might otherwise be constants. PHP constants have some things about that that are pretty annoying, and they aren't highly used. The main argument in favor of their use is that they can't be changed once they are defined (with the define() function) but when you have a page scope language, the concern about someone changing a variable like that isn't that great, and instead the OOP data structure that most people favor these days is to instead use a config class that loads configuration variables and makes them available throughout the application. One example of this idea is the zend framework config class which supports a bunch of different file formats where you can setup what would otherwise be constants. http://framework.zend.com/manual/en/zend.config.html Something like a base path is often derived from having the controller or bootstrap run some variation of: $basepath = dirname(__FILE__); Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205133 Share on other sites More sharing options...
gizmola Posted April 23, 2011 Share Posted April 23, 2011 Just now seeing your last post, it might help you to have it stated that the php include/require functions are all file system based. They are rely on knowing where the other files are on the server. This allows you to do things like include files from directories that aren't accessible via a url (they are outside of webspace). Url's are all based on webspace ... so they are all relative to the home directory for your website. So an is always going to be relative to your website. Typically people have static directories for images, javascript and .css so that you can always specify an absolute/relative url for them like: Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205134 Share on other sites More sharing options...
doubledee Posted April 23, 2011 Author Share Posted April 23, 2011 Hey Debbie, PHP has page scope. People either love that or hate it, but it's a basic principle of the language in web application use. Variables exist for the lifetime of the script and not beyond. I think I probably alluded to this a while back, in a reply to one of your earlier posts, but this is why it is good to have a controller script that is the basis of your site... I remember that, but when you said "Controller" I immediately thought "OOP" and "MVC" and I quickly ran as far away as possible!!!!! (I'd love to learn OOP the next go-around and build a more robust website, but I'm not ready for that yet.) My website looks pretty fancy, but it is really a bunch of pretty HTML pages with dashes of PHP sprinkled in, so I'm trying to keep things as simple as possible for now... Maybe you'll have some opinions and recommendations based on what I just posted above?? Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205135 Share on other sites More sharing options...
Pikachu2000 Posted April 23, 2011 Share Posted April 23, 2011 In a php script, you can normally use $_SERVER['DOCUMENT_ROOT'] as the starting point then add the rest of the path to it. For php scripts, the path is based on the filesystem of the server, starting at the disk root. For html output sent to the browser, forward slash / denotes the web root. That is because when you send a tag like <img src="/images/pic.png">, the web browser initiates a request for the image resource, and to the browser the / denotes the root of the website. If your website resides (for example) in /var/www/html/my_site/, and in there you have an img/ directory containing an image, in php you could access it via $_SERVER['DOCUMENT_ROOT'] . '/img/image.png' In an <img> tag it would be <img src="/img/image.png"> Hope that explanation helps. Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205137 Share on other sites More sharing options...
gizmola Posted April 23, 2011 Share Posted April 23, 2011 Yes MVC requires oop but a simple controller only requires a switch statement and typically reads one or 2 parameters total. I'm not going to twist your arm, but you did ask for a solution to the problem of having multiple different files. Here is what I'd suggest. Use the technique I just described inside a file in the root (where your index.php file is) in a script you call config.php. Notice that the special property of http://php.net/manual/en/language.constants.predefined.php __FILE__ is that it will provide the path of the file that contains it, no matter where that file on the file system. In your scheme you know that this file will always be in your root directory, so anything you use it for will always be relative to it. You can then specify include($basepath . '/components/body_header.inc.php'); And know that no matter what script your talking about you will have the right include. The only fly in the ointment is that you need to know for any script where to get the config.php from, in a relative sense. For index.php it's simple: require_once('config.php'); For your checkout.php script: require_once('../config.php'); Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205138 Share on other sites More sharing options...
doubledee Posted April 23, 2011 Author Share Posted April 23, 2011 In a php script, you can normally use $_SERVER['DOCUMENT_ROOT'] as the starting point then add the rest of the path to it. For php scripts, the path is based on the filesystem of the server, starting at the disk root. Let's see if I can get this... On my MacBook, I have... Users/user1/Documents/DEV/++htdocs ...as my "web root", right? (I'm pretty sure that is what MAMP thinks.) And then NetBeans added on another layer of "00_MyWebsite" as a project folder, so that is a pseudo-Web Root I guess?! In NetBeans, all of my files sit in "00_MyWebsite", so I think of that as the "web root"... Now in "checkout.php" when I say... (file "/00_MyWebsite/secure/checkout.php") <?php require_once('/00_MyWebsite/components/body_header.inc.php'); ?> Shouldn't that point to my Web Root (i.e. "++htdocs") and then absolutely point to "body_header.inc.php" and work?? Well, it doesn't! The include fails. This works... <?php require_once('../components/body_header.inc.php'); ?> So does this... $path = $_SERVER['DOCUMENT_ROOT'] . '/00_MyWebsite/'; require_once($path . 'components/body_header.inc.php'); Not sure about this one. When I echo $path I get... path = /Users/user1/Documents/DEV/++htdocs/00_MyWebsite/ I'm confused. For html output sent to the browser, forward slash / denotes the web root. That is because when you send a tag like <img src="/images/pic.png">, the web browser initiates a request for the image resource, and to the browser the / denotes the root of the website. So on my laptop, is that "++htdocs" or "++htdocs/00_MyWebsite"?? If your website resides (for example) in /var/www/html/my_site/, and in there you have an img/ directory containing an image, in php you could access it via $_SERVER['DOCUMENT_ROOT'] . '/img/image.png' In an <img> tag it would be <img src="/img/image.png"> In my "body_header.inc.php" file, this seems to work for all files... <a href="index.php"> <img class="logo" src="/00_MyWebsite/images/my_logo.png" width="220" /> </a> Is there a better way? Hope that explanation helps. It is a good start!! Thanks, but we're not out of the woods yet!! Debbie P.S. If you are off to bed, I hope you can check back in the morning?! Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205140 Share on other sites More sharing options...
wildteen88 Posted April 23, 2011 Share Posted April 23, 2011 Let's see if I can get this... On my MacBook, I have... Users/user1/Documents/DEV/++htdocs ...as my "web root", right? Yes that is correct. This is what $_SERVER['DOCUMENT_ROOT'] will be set to. You can confirm this by running echo 'My webroot path is: ' . $_SERVER['DOCUMENT_ROOT']; And then NetBeans added on another layer of "00_MyWebsite" as a project folder, so that is a pseudo-Web Root I guess?! In NetBeans, all of my files sit in "00_MyWebsite", so I think of that as the "web root"... No, that is incorrect. netbeans will not alter your websites webroot. All netbeans did was add a folder to your webroot folder called 00_MyWebsite. This folder contains all files for your site (php/html/java scripts, images etc). As all your files are contained within this folder I'd consider setting a variable which sets 00_MyWebsite as the webroot. Like so <?php $MY_WEBROOT = $_SERVER['DOCUMENT_ROOT'] . '/00_MyWebsite'; ; When performing any includes we'll prepend this variable in front of our file paths. Like so <?php require_once($MY_WEBROOT . '/components/body_header.inc.php'); ?> Above code should now included body_header.inc.php successfully. This works... <?php require_once('../components/body_header.inc.php'); ?> That is because you used a relative file path, instead of an absolute file path. Above we're dealling with absolute file paths Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205195 Share on other sites More sharing options...
doubledee Posted April 23, 2011 Author Share Posted April 23, 2011 I created these constants... <?php define('ENVIRONMENT', 'development'); // Change to 'production' when ready. define('ROOT', ENVIRONMENT === 'development' ? '/Users/user1/Documents/DEV/++htdocs/00_MyWebsite/' : '/var/www/vhosts/MyWebsite.com/httpdocs/'); define('WEB_ROOT', ENVIRONMENT === 'development' ? 'http://localhost/00_MyWebsite/' : 'http://www.MyWebsite.com/'); ?> ...and they seem to work with my Includes, but the link to my Logo is still broken!! Below is the problematic code. The commented out code did work, but my new code using one of my Constants breaks thinks again?! <a href="index.php"> <img class="logo" src="<?php WEB_ROOT ?> images/my_logo.png" width="220" /> <!-- <img class="logo" src="/00_MyWebsite/images/my_logo.png" width="220" /> --> </a> The code above is supposed to make the logo path ABSOLUTE so it doesn't get broken by my include, but that isn't happening... Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205271 Share on other sites More sharing options...
wildteen88 Posted April 23, 2011 Share Posted April 23, 2011 Probably due to the space just before images/ on this line <img class="logo" src="<?php WEB_ROOT ?> images/my_logo.png" width="220" /> Remove the space and your logo should work. Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205274 Share on other sites More sharing options...
PFMaBiSmAd Posted April 23, 2011 Share Posted April 23, 2011 You also need an echo statement with that - <?php echo WEB_ROOT ?> Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205277 Share on other sites More sharing options...
doubledee Posted April 23, 2011 Author Share Posted April 23, 2011 Probably due to the space just before images/ on this line <img class="logo" src="<?php WEB_ROOT ?> images/my_logo.png" width="220" /> Remove the space and your logo should work. That was one problem... Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205278 Share on other sites More sharing options...
doubledee Posted April 23, 2011 Author Share Posted April 23, 2011 You also need an echo statement with that - <?php echo WEB_ROOT ?> I am such an idiot! Thanks to both of you!! (More like, ALL OF YOU!) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205279 Share on other sites More sharing options...
doubledee Posted April 23, 2011 Author Share Posted April 23, 2011 Stepping back... I have just wasted a day and a half of my life, and I'm not sure what I've accomplished... :-\ index.php product_details.php secure/checkout.php all work (including the logo), so that's good. And if my code works seamlessly on my Web Host, I guess that is better. But did I really accomplish anything creating my ROOT and WEB_ROOT constants? It sorta seems like I just did things another way, but I dunno. I mean now I have to put this in every file which is more work?! <!-- Access Constants --> <?php require_once "config.inc.php"; ?> And I still have to use relative paths to that Config file, since my Constants haven't been defined yet. And I also can't benefit from any of this here... <head> <!-- Include HTML Metadata --> <?php require_once "components/html_meta.inc.php"; ?> ...since it is before I include the Config file. So did I really accomplish anything?? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205280 Share on other sites More sharing options...
gizmola Posted April 23, 2011 Share Posted April 23, 2011 There is no reason for you not to include the config file first, and not using it everywhere defeats the purpose. As for what you accomplished -- would you rather manually type in the full path to every included script you need in every script? Along with all the extra work, you open up the possibility that you'll make a mistake and have to debug why something that works in several scripts doesn't work in a 3rd. In software engineering there's a term for this idea: DRY At some point let's assume that you move your files from one host to another. Do you think going through every file and looking for/changing several include paths is a good way to go? Web development is difficult and there's a lot to learn. Just putting things in perspective for a minute, what you describe as taking a day and a half would take an experienced php developer literally 5 minutes. Please don't misinterpret my meaning -- most of the people who have been answering your questions are professional web developers who studied computer science, or have spent literally years working with php. It would be unreasonable to expect that you're going to be either proficient or efficient when you're just learning the basics. In my opinion, you seem overly concerned with these questions -- as if you're expecting to at any minute stumble across some secret that reveals to you that everyone here is an idiot, and there's actually a magic button you can push and websites pop out. Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205304 Share on other sites More sharing options...
doubledee Posted April 23, 2011 Author Share Posted April 23, 2011 There is no reason for you not to include the config file first, and not using it everywhere defeats the purpose. I guess. (I was just envisioning defining the Constants once and having them accessible from anywhere.) As for what you accomplished -- would you rather manually type in the full path to every included script you need in every script? Along with all the extra work, you open up the possibility that you'll make a mistake and have to debug why something that works in several scripts doesn't work in a 3rd. I guess. (Just frustrated it took me over a day to get how include paths and HTML paths work?!) In software engineering there's a term for this idea: DRY At some point let's assume that you move your files from one host to another. Do you think going through every file and looking for/changing several include paths is a good way to go? Good point. Web development is difficult and there's a lot to learn. I agree! MUCH MUCH harder than it looks!!! Just putting things in perspective for a minute, what you describe as taking a day and a half would take an experienced php developer literally 5 minutes. Please don't misinterpret my meaning -- most of the people who have been answering your questions are professional web developers who studied computer science, or have spent literally years working with php. It would be unreasonable to expect that you're going to be either proficient or efficient when you're just learning the basics. I know, but it hurts when it takes me over a day what takes you guys 5 minutes?! In my opinion, you seem overly concerned with these questions -- as if you're expecting to at any minute stumble across some secret that reveals to you that everyone here is an idiot, and there's actually a magic button you can push and websites pop out. I never called anyone an idiot... I'm just frustrated how long it took me to get how includes and HTML links work. (I think its dumb how when I include a file it changes the orientation to what was linked to it.) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205314 Share on other sites More sharing options...
gizmola Posted April 23, 2011 Share Posted April 23, 2011 Most of us experience these types of things at one time or another. I've said this many times over the years, but I don't mind admitting that despite having been a professional programmer at numerous software companies, and having worked on the development of hundreds of websites used by millions of people I still learn something new about web development on weekly if not daily basis. For me it's part of the fun that it continues to offer a challenge, but people regularly underestimate how complicated it is thanks to books that claim it's "easy". If you go back in the thread and look at my earlier reply, I did point out that the includes deal with files and are relative to the filesystem of the server, while url's inside html are relative to the webroot. Maybe that is not as clear as I think it is, but getting clarity on that will save you a lot of banging your head against the wall. The webroot is webserver magic. I often call this "webspace". If something is in webspace you can type in a url like "http://www.mysite.com/something" and it will return that to you, whether it be an image, .css, .js, .html or .php. Deconstructing that, the webroot is "/". So when you're dealing with things that are sent in html, it's always relative to the webroot. It doesn't matter that the real path on the server (or your workstation with WAMP) is /user/me/documents/00_site/ because the webserver takes care of that, so inside your html the only thing that is ever important is where that exists BELOW the webroot. Since php includes are files from the filesystem, those functions, along with any of the file reading or writing functions, ALWAYS are relative to the filesystem. They don't know or care about the webroot or webspace. They need to know where is that file inside the filesystem on this machine that is running apache/php. If you can keep those two separate systems clear in your mind, you'll be breezing through includes and urls. Quote Link to comment https://forums.phpfreaks.com/topic/234488-questions-about-constants/#findComment-1205329 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.