PHP help and guide

Had your computer crash on you, or a website shows wrong, or your printer went dead on you? Come on in...

Moderator: Crew

Post Reply
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

PHP help and guide

Post by Chroelle »

OK.

Now if you know me just a little bit, then you propably know that I am an expert of screwing up technical things. So if ever there was a person to write a guide, then I am certainly not it.

HOWEVER.....

I have decided that now is the time to learn PHP, and since my workplace wants to donate some time for me to learn it, I am going to do it this time around...
I decided that if I also started this guide that there would be no way out....

So if you want to learn PHP (the basics) just follow me in here. I will write in here as I learn stuff. Any questions you have, please feel free to ask. Also please correct me if I am wrong.

Guide will be started shortly.
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
User avatar
cielorosso
Member
Posts: 52
Joined: Mon Feb 20, 2006 23:36
Location: fere semper ad Universitas Arhusiensis
Contact:

Post by cielorosso »

Sites that i've found to be helpful:

http://php.net (of course ;) )
http://www.w3schools.com/php/default.asp
http://www.goodphptutorials.com/

There are a lot more, but these 3 helped me the most :D
IUSTITIA OMNIBUS
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

Post by Chroelle »

Here goes my own version.

First lesson
Follow my progress on http://www.connect-webdesign.dk/PHP.php

First off lets start with saying that PHP is a coding language for making specific actions. A PHP-file is actually just a HTML-file with PHP-language in between.

I will make you an example:
<HTML>
<Body>
1. General world oppinion:<br>
<SCRIPT LANGUAGE="PHP">
echo "CWF Rocks";
</SCRIPT>
</body>
</html>

That is the first version of what I did on the linked page.

I ask the server to use PHP-language when reading the lines I have written. The < "Script language"> means that I am starting a request, while the </Script language> later ends it. Please notice that I dont need to write ="PHP" after SCRIPT LANGUAGE as it figures that out by itself.

The "echo" tells the server what to write after the PHP.
Also it is very important to notice the ; in the end of my PHP-line as it is the end of the echo. You might have seen this replaced by PRINT at some point. Basically echo is the output for the user.

There are other ways to make phplines than the one above. I will copy/paste the entire codesection of what I wrote in my PHP-file that made the result you can see by following the link.


<br>
<HTML>
<Body>
2. General world oppinion:<br>
<?PHP
echo "CWF Rocks";
?>
</body>
</html>

<br>
<HTML>
<BODY>
<?php
echo "And they are right";
?>
</HTML>
</BODY>

These lines should make it possible to see how to do PHP in other ways.

You can also make remarks for yourself in the middle of the text. This can be done by typing
// this is a remark (This type of remarkk ends along with the line, so when you change lines you end the remark/comment.
/* this type of comment can be made over several lines and hence has to be ended by writing */
#This type of comment ends with the line also.
I did not make any comments in the text I did in the link.

Things learned the hard way:
1. You need a webhotel or some way of placing the text on a server with PHP installed. Otherwise you can download and install it on your own computer, but that is tedious, and I dont want to go into that...

2. When making html documents with PHP in them you should always remember to make sure you saved that file with a .php extension. I had a hard time figuring out that I forgot that, and tried desperately to make the sever read my .txt-document as a .php-document.

Please ask if you have any questions. Next lesson will follow when I get there :D
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS
Contact:

Post by Maz »

Few comments / tips. (I try to give some hints between Curlies posts :) )

It is suggested to use

<?php and ?> as opening and closing tags. Those are the most standard notations.

It is also advieced to not mix php and html in same file. You can do your html output by using echo or print statements.

IE:

<?php

echo '<html><head></head><body>';
/*

Lots of stuff

*/
echo '</body></html>';
?>


Also the fact that php files are processed by php interpreter, and after processing the real output is given to browser includes a danger. Let's assume you use a database and need a password to connect to it. (Or you need some other password in your script)
Now it is obvious that you do not wish to give the password to people that browse your site. As you propably know, php code is not usually shown in browser, but the output produced by code is. IE if you select view -> source from your browser, you see the output chroelle's php script gave, not the echo statements. So it gives you a feeling of safeness. Your password whis is used in script, not echoed to browser is not viewed. But what happens if for some reason the php interpreter crashes on server where your script is hosted? Well, it may be that your entire code is outputted to user's browser..

That's why you should always do things that require passwords in separate files, which are placed in a folder not accessible from web. (protected by .htaccess file for example). Then you use include('filename.php'); function to 'copypaste' the code in such a file into the code that is being accessed by browser. If php interpreter now fails. the include('filename.php'); will not be executed, and password sensitive code is not 'pasted' to user.

Also, notice that the file-extension may not always be php. It can be configured in server's php settings. (Although it usually is php). It is also possible that server has older and newer versions of php installed (like php4 and php5), and server decides which one to use based on filename. In such situations it is quite common that file extension to use specific version of php is set to be .php5 or .php4, while .php is usually kept as extension for the default version.
Post Reply