Jump to content
xisto Community
Chesso

Some Little Php Tips.

Recommended Posts

I think this is probably my first article/tutorial on here B).

 

I have been playing around with a new site lately, and co-incidentally, as soon as I started with the PHP etc, I have people on MSN Messenger flooding me with questions, mostly in the form of "help me, help me" etc.

 

Well, a few small tips and explanations on them I gave over MSN Messenger, gave me the idea of posting an article/tutorial (or whatever you want to call it) here about it.

 

RELATIVE PATHING:

 

The first thing to get out of the way, is relative pathing.

 

I would hope that most people are already using this, but surprisingly many newer web developers aren't (I do of course as I come from a heavy list of general programming and scripting language background).

 

The basic essence of relative pathing is merely using path's to a file, folder etc in a "relative" rather than "full" manner, for example:

 

Let's say you are messing around in the file editor of your Asta Host control panel, you would normally put you index file in the WWW folder (or at least I do), so let us assume that you are working with your index file in this folder for ease of use.

 

Some people may use a "full" path to access, say, there contact page (which also resides in the WWW directory directly) like so:

 

http://forums.xisto.com/no_longer_exists/

But this is completely unnecessary, we can instead, use "relative" pathing like so:

 

contact.html

Wow it's much shorter isn't it? This is because (ideally in this article/tutorial) you have linked this in your index file, we are working with a file that resides in the exact same directory as our contact file, so we don't need to do anything but reference the actual filename.

 

However, what to do if the file you wish to access resides in directories deeper from the file we are working with, or perhaps the file resides in directories higher up from the file we are working with.

 

Well in the case of the former:

 

myfolder/myfile.html

Of course if it were several folders deeper, we would add them also, it's no differant then a full path, except that we remove the web address from the picture.

 

Now in the case of the latter:

 

../myfolder/myfile.html

This one takes a bit of a different approach, the ../ basically tells it to go backwards by one directory/folder.

 

So for example, you have a members folder in your WWW folder, you are working in members.html or what ever, but you need to access/include a database connection file from the DB folder which is in the WWW folder also.

 

In this case, we do like above, ../ from our members folder (which will takes up back to the WWW folder), ../DB (takes us back to the DB folder in the WWW folder) ../DB/file.php (should be obvious now :) ).

 

If you want to go back more than 1 directory/folder, just use more ../'s.

 

 

USING INCLUDE:

 

There is more than 1 way to use this, and several different calls, but primarily I just use include 'file';

 

This statement is very much like using external CSS and Javascript (although obviously different), but a similar concept. It will literally "include" all code from the file you specify inbetween the quotes, as if it was there to begin with.

 

Why is this useful?

 

Well we could easily split our sites layout up into several files, I personally have a header, menu and footer (the main content file being the one we use the include statements in).

 

You'll basically want to move any header specific code to a seperate file, menu specific code to a seperate file and footer specific code to a seperate file. Don't worry, other than this, there is no other change necessary what so ever (you don't need to go messing around with opening/closing html tags or php tags etc, as I said, it will literally include the code from the file, as if it had been placed in there manually).

 

This is extremely usefull, as we can re-use code from these files, in all of our main files, just like we do with external CSS and Javascript files. And as you might already imagine, 1 change to any of these external files we include, all of our "main" files including them will be all updated automatically (no need to manually edit every single main file).

 

Ok, so whether you are running things locally (with something like WAMP Server or EasyPHP), or doing things via the file editor in the Asta Host hosting control panel (CPanel), let's do a very simple test that should help enlighten you as to what exactly happens.

 

You will need to create 2 PHP files, name 1 of them main.php (our core file in this test), and header.php (the file we will include from our main.php file).

 

In the header.php file, put:

 

<?php echo "<h1>OUR HEADER</h1>"; ?>

And in our main.php file, put:

 

<?php include 'header.php'; ?><p>Look our header has been *included* above.</p>

After saving the changes to these files after there creation, go ahead and try accessing main.php, hopefully you should get the h1 HTML code place at the very top of the generate page, and our little paragraph below it.

 

As we used the include statement to include our header.php file above the paragraph html, it of course is generate first and above.

 

 

Well that's it for now, this has ended up much longer than I had intended but I tried to write it so most people should be able to understand it. I was going to add some explanatory material on creating what I call "constants" and using them in conjunction with both "relative pathing" and "file includes".

 

That particular part, would basically help you to have core header, menu, footer etc files in your main (or WWW directory if that's the case) and yet be able to access these in pages in other folders still using "relative pathing" as oppose to "full pathing".

 

Any comments, feedback, suggestions etc would be greatly appreciated (especially when I one day intend to build up a site filled with such articles/tutorials on many different programming and scripting languages).

Edited by Chesso (see edit history)

Share this post


Link to post
Share on other sites

Instead of using include, you should use require, like this:

<?php require('header.php'); ?><p>Look our header has been *included* above.</p>

That means if header.php isn't found, then it will generate a fatal error, which ensures that your users don't see crap such as Warning: blah blah blah cannot be found in home/some file....

Share this post


Link to post
Share on other sites

There is also: include_once, the name explain its self, will ensure to avoid "include loops".PHP functions: "require" and "include", are similar but have different purposes. I never use require because i don't think that is neccesary to be agresive and stop the script. Manage missing files using file_exists, is more "smart crashing" friendly. You don't have to impact your users and create an impression of CAOS. A very well writen, beauty and explaining message will help to fight with the stress and may safe your job B). Is not a joke!!!Have a good coding and blessings!

Share this post


Link to post
Share on other sites

Correct you would have to be extremely careless to require using "require". I also do not use include_once as I don't do any loops anywhere near an include.All testing on my end is done locally, uploaded when ready, and then re-tested online before making the changes etc public. Seems to work best for me.

Share this post


Link to post
Share on other sites

In fact, using include, include_once and require - they aren't the same and they have a purpose for that, for example, if you have a file full of classes and functions, it is very good to include_once, due to if in some way the file will be included again, you'll get errors, due to you included the same function names and etc. also sometimes you need in another place by another value to include the same file again with a different value and that files has an include to functions, so if it is included the second time, you'll get errors, but by using include_once, everything will execute great.. require is good too, especially with very important files, if without them the script might give away something what you don't want to give away and you want to stop the execution..

But for small sites, in my opinion include just is good, but when you'll start programming big projects, over at least 100kb of code you'll understand where and why and what you want to use, it would be stupid to have all those functions (include, require, include_once, require_once) if they didn't do anything special or we could live without them (we can live without them, but eh..)

I myself, now include files like this, for example:

if (FALSE === (include "siteTemplate" . substr(strtoupper($theSite['url']['protocol']), 1). ".php")) {	echo 'Site Template for <b>' . $theSite['url']['protocol'] . '</b> Protocol could Not be Found on the Server';}

and I don't need to check if the file exists or not and even sometimes, if it exists, the file can't be included, besides, checking every file if it exists is a waste of resources, especially if you have lots of things/files in your script to use.. require, include_once, require_once can be used to check for === FALSE too B)
Edited by Quatrux (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.