[17:13] <@qwertydawom> [17:13] <@qwertydawom> == [17:13] <@qwertydawom> <% %> [17:13] <@qwertydawom> [17:14] <@qwertydawom> The most used is : [17:14] <@qwertydawom> although it's not the better choice [17:14] <@qwertydawom> The best one (to avoid future problems) is : [17:15] <@qwertydawom> The "<% %>" are really depricated, and you must avoid them (unless you have a compatibility problem) [17:15] <@qwertydawom> (with an asp editor) [17:16] <@qwertydawom> The first thing you have to know is : [17:16] <@qwertydawom> each syntax is always ended with a ";" [17:16] <@qwertydawom> if you forget it, you'll get a : PARSE ERROR [17:16] <@qwertydawom> Now, the first script.. like usual.. "hello world" ;) [17:17] <@qwertydawom> echo 'Hello world !' ; [17:17] <@qwertydawom> ?> [17:17] <@qwertydawom> Like you see, we use "echo" to print some text [17:17] <@qwertydawom> Now, if you want to format this text (i.e. : make it look nicer :P) [17:18] <@qwertydawom> you have to use html [17:18] <@qwertydawom> For example : [17:18] <@qwertydawom> echo 'Hello world !'; [17:18] <@qwertydawom> ?> [17:18] <@qwertydawom> It will print a red "hello world" :) [17:18] <@qwertydawom> So, what if we want to add an image with the text? [17:18] <@qwertydawom> html again [17:18] <@qwertydawom> echo '
Hello world !
'; [17:19] <@qwertydawom> echo '
'; [17:19] <@qwertydawom> ?> [17:19] <@qwertydawom> This will print a blue "hello world", and display an image next to it :) [17:19] <@qwertydawom> Therefore, you clearly see it's important to know html before you start learning php [17:20] <@qwertydawom> For this lecture, I'll be assuming you know it. [17:20] <@qwertydawom> echo and print() [17:20] <@qwertydawom> The first one is a php pre-built function [17:21] <@qwertydawom> that's why you don't need to put the brackets [17:21] <@qwertydawom> and, unlike print() you can put more than one parameter [17:21] <@qwertydawom> separated with commas [17:22] <@qwertydawom> Echo is slightly faster than print(), that's why we'll use this one :) [17:22] <@qwertydawom> here's an example on how to use these functions : [17:22] <@qwertydawom> echo 'text'; [17:23] <@qwertydawom> echo 'text','textagain'; [17:23] <@qwertydawom> print('text'); [17:24] <@qwertydawom> If we put : [17:24] <@qwertydawom> echo 'I'm using php'; [17:25] <@qwertydawom> we'd get : [17:25] <@qwertydawom> Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in yourfile.php on line 2 [17:25] <@qwertydawom> what we have to do to correct it [17:25] <@qwertydawom> is "escape" the ' char [17:25] <@qwertydawom> to do so, we use a backslash [17:25] <@qwertydawom> e.g. : [17:25] <@qwertydawom> echo 'I\'m using php'; [17:26] <@qwertydawom> But, you'll ask : what if I want to put a \ in my sentence...? [17:26] <@qwertydawom> well, you'd just have to put another \ before it :) : \\ [17:26] <@qwertydawom> There are also other special chars : [17:27] <@qwertydawom> \t : tabulation [17:27] <@qwertydawom> \r : carriage return [17:27] <@qwertydawom> \n : new line [17:27] <@qwertydawom> But, those special chars will always work in sentences within double quotes [17:27] <@qwertydawom> For example you can do so : [17:27] <@qwertydawom> echo 'some text',"\n"; [17:28] <@qwertydawom> Or, more practical : [17:28] <@qwertydawom> echo "some text\n"; [17:28] <@qwertydawom> :) [17:28] <@qwertydawom> Well, we're done with the first part [17:28] <@qwertydawom> Time has come for you to ask your questions! [17:28] what's the difference between single quotes and double quotes? [17:28] <@qwertydawom> good question :) [17:28] Correction: It's parenthesis not brackets in the print function [17:28] <@qwertydawom> in fact [17:29] <@qwertydawom> Correction: It's parenthesis not brackets in the print function --> thanks ;) [17:29] <@qwertydawom> single quotes are used when you want to print text only [17:29] <@qwertydawom> double quotes are used when there are variables involved [17:29] if you went print '@array' it would print @array, if you went print "@array", it would print the contents of array [17:29] concantating [17:29] like, echo "aaa" . var . "more"? [17:30] ah [17:30] <@qwertydawom> well, then, you could think : why not using only double quotes? [17:30] inline, ok [17:30] * Ch4r sets mode: -q mov_21h [17:30] * Ch4r sets mode: -qo Ch4r Ch4r [17:30] <@qwertydawom> because when you use it, php checks for every word to see if there is a variable [17:30] -> -Ch4r- good move ;) [17:30] <@qwertydawom> therefore, that makes it longer [17:30] <@qwertydawom> so : only text -> single [17:30] <@qwertydawom> variables -> double [17:31] right, thanks then [17:31] <@qwertydawom> ok, np :) [17:31] Will loops and classes and object sbe discussed later? [17:31] <@qwertydawom> Loops, yeah [17:31] <@qwertydawom> classes and objects, not this time [17:31] Oky doky. [17:32] <@qwertydawom> so, I assume I can move on :) [17:32] <@qwertydawom> II- Date/Time [17:33] <@qwertydawom> With php, it's really simple to know the date of today, but also to know which day it will be in 453 days for example, and reciprocally in the past. [17:33] <@qwertydawom> Now, let's see a simple date [17:34] <@qwertydawom> and, we'll know use our first variable. :) [17:34] * Joins: Reikon_ (Reikon@Shellcoder-A71AE4F0.cfl.res.rr.com) [17:34] <@qwertydawom> (all the variables begin with the dollar sign : $) [17:34] <@qwertydawom> $date = date("Y-m-d"); [17:34] <@qwertydawom> $hour = date("h:i"); [17:34] <@qwertydawom> Print("Today is $date and it's $hour"); [17:34] <@qwertydawom> ?> [17:35] <@qwertydawom> That will print : [17:36] <@qwertydawom> Today is 2005-08-28 and it's 11:36. [17:36] <@qwertydawom> So, it is the date() function that's used to get the date from the server [17:36] <@qwertydawom> but, take care, because the hour is in function of the position of the server itself! [17:36] <@qwertydawom> alright ;) [17:37] <@qwertydawom> so, I've used : Y-m-d [17:37] <@qwertydawom> but, you can change the order if you like [17:37] <@qwertydawom> Now, let's see the parameters you can put into it : [17:38] <@qwertydawom> a : am or pm [17:38] <@qwertydawom> A : AM or PM [17:38] <@qwertydawom> d : day with two digits (from 01 to 31) [17:38] <@qwertydawom> D : day of the week, with three letters (e.g. : Fri) [17:39] <@qwertydawom> h : hour, 12 hours format (01 to 12) [17:40] <@qwertydawom> H : hour, 24 hours format (00 to 23) [17:40] <@qwertydawom> g and G are respectively the same, but, without the initial zeros [17:40] <@qwertydawom> i : minutes (00 to 59) [17:40] <@qwertydawom> i : minutes (00 to 59) [17:40] <@qwertydawom> j : day of the month without the initial zeros [17:41] <@qwertydawom> l (little "L") : day of the week, fully written [17:41] <@qwertydawom> m : month (01 to 12) [17:41] <@qwertydawom> n : month without the initial zeros [17:42] <@qwertydawom> M : month, with three letters [17:42] <@qwertydawom> s : seconds (00 to 59) [17:43] <@qwertydawom> III- PHP within an HTML code [17:44] <@qwertydawom> Warning : since you put php code with a .html (or .htm) document [17:44] <@qwertydawom> you'll have to rename this file to *.php or .phtml (although the first one is mainly used) [17:45] <@qwertydawom> [17:45] <@qwertydawom> [17:45] <@qwertydawom> The text in HTML [17:45] <@qwertydawom> //The PHP code [17:45] <@qwertydawom> $hour = date("H\hi"); [17:45] <@qwertydawom> print(" and the PHP one."); [17:45] <@qwertydawom> ?> [17:45] <@qwertydawom> [17:45] <@qwertydawom>
It's . [17:45] <@qwertydawom> [17:45] <@qwertydawom> [17:45] <@qwertydawom> So, with the above example, you can see how html and php fit well :) [17:46] <@qwertydawom> Like you've seen, in php, to put comments, you'll have to use : // [17:46] <@qwertydawom> If it your comment is longer than one line [17:46] <@qwertydawom> you can use : /* your comment here */ [17:47] <@qwertydawom> Now, let's talk about the include() function [17:47] <@qwertydawom> If your html code is long, it'll look messy if you insert php within it [17:47] <@qwertydawom> that's why we'll use this function [17:47] <@qwertydawom> [17:47] <@qwertydawom> [17:47] <@qwertydawom> The text in HTML [17:47] <@qwertydawom> include("qwerty.inc.php"); //we call the file [17:47] <@qwertydawom> ?> [17:47] <@qwertydawom> [17:47] <@qwertydawom> [17:48] <@qwertydawom> $hour = date("H\hi"); [17:48] <@qwertydawom> print("
and the PHP one. It's $hour.
"); [17:48] <@qwertydawom> ?> [17:48] <@qwertydawom> The second code is the one there is into : qwerty.inc.php [17:48] <@qwertydawom> You'll have noticed the included files' extension is : *.inc.php [17:49] <@qwertydawom> this is for a better readability [17:50] <@qwertydawom> IV- Concatenation [17:50] <@qwertydawom> The dot is used to concatenate strings. [17:51] <@qwertydawom> Let's take an example where a text has to be sticked next to a variable : [17:51] <@qwertydawom> $date = gmdate("H\hi"); [17:51] <@qwertydawom> print("It's $date"."gmt."); [17:51] <@qwertydawom> ?> [17:51] <@qwertydawom> So, the server won't be fooled and think the variable's name is "dategmt" [17:51] <@qwertydawom> print("It's $date"."gmt."); [17:51] <@qwertydawom> ?> [17:51] <@qwertydawom> So, the server won't be fooled and think the variable's name is "dategmt" [17:52] <@qwertydawom> Notice the second dot is placed within the quotes, so, it'll be printed as a dot :) [17:53] <@qwertydawom> Now, and, I'm sure SysSpider will like it, we're going to see the difference between single and double quotes (with examples of code) :) [17:53] <@qwertydawom> $name = "Qwerty"; [17:53] <@qwertydawom> echo "My name is $name"; [17:53] <@qwertydawom> ?> [17:53] <@qwertydawom> This will print : [17:53] <@qwertydawom> My name is Qwerty [17:54] <@qwertydawom> $name = "Qwerty"; [17:54] <@qwertydawom> //with simple quotes [17:54] <@qwertydawom> echo 'My name is $name'; [17:54] <@qwertydawom> ?> [17:54] <@qwertydawom> This one would print : My name is $name [17:54] <@qwertydawom> $name = "Qwerty"; [17:54] <@qwertydawom> //with simple quotes [17:54] <@qwertydawom> echo 'My name is '.$name; [17:54] <@qwertydawom> ?> [17:54] <@qwertydawom> And this one : My name is Qwerty [17:55] <@qwertydawom> So, with the previous explanation and the above code, I think you've understood the difference between them :) [17:55] <@qwertydawom> Right now, you'll see it's possible to concatenate directly a function and a string : [17:55] <@qwertydawom> print('Today is '.gmdate('d-m-Y').'...'); [17:55] <@qwertydawom> ?> [17:56] <@qwertydawom> So, it'll does the same as the first code presented in this section [17:56] <@qwertydawom> but, the code is reduced to one line [17:56] <@qwertydawom> which can be nice if we're writing a looong code [17:57] <@qwertydawom> but, I have to admit this one is less readable for someone who really begins [17:57] <@qwertydawom> so, that's you to choose :). [17:58] <@qwertydawom> Now, if you want to call an url with variables in it [17:58] <@qwertydawom> concatenation will be useful [17:58] <@qwertydawom> look at those two codes : [17:58] <@qwertydawom> $file = "file.php?var=$var&data=$data"; [17:58] <@qwertydawom> ?> [17:58] <@qwertydawom> and [17:58] <@qwertydawom> $file = 'file.php?var='.$var.'&data='.$data; [17:58] <@qwertydawom> ?> [17:58] <@qwertydawom> I am not saying the first one doesn't work [17:58] <@qwertydawom> but, it's a bit deprecated [17:59] <@qwertydawom> and, it's better to begin with good habits ;). [18:00] <@qwertydawom> V- Get values from a form [18:01] <@qwertydawom> When one of your visitors enters informations in a form [18:01] <@qwertydawom> there are 'transformed' into variables [18:02] <@qwertydawom> The name of those variables depends on the sending method of the form. [18:02] <@qwertydawom> Since in our next code, the method will be POST (the other one is GET) [18:02] <@qwertydawom> Since in our next code, the method will be POST (the other one is GET) [18:03] <@qwertydawom> you have to put as a name : $_POST['name_of_the_field'] [18:03] <@qwertydawom> So, let's see an example! :) [18:03] <@qwertydawom> Here's the html form : [18:04] <@qwertydawom> [18:04] <@qwertydawom>
[18:04] <@qwertydawom> Surname :
[18:04] <@qwertydawom> Firstname : [18:04] <@qwertydawom> [18:04] <@qwertydawom>
[18:04] <@qwertydawom> And here's the php code for "check.php" : [18:04] <@qwertydawom> $firstname = $_POST['firstname']; [18:04] <@qwertydawom> $name = $_POST['name']; [18:04] <@qwertydawom> print("
Hey $firstname $name
"); [18:04] <@qwertydawom> ?> [18:05] <@qwertydawom> If you enter something within the firstname and surname, it will display (for example) : [18:05] <@qwertydawom> Hey Qwerty Dawom [18:05] <@qwertydawom> Now, obviously, we'll have to check what the user enters, to avoid errors [18:06] <@qwertydawom> The first function we'll use is the empty() function which checks if a field is.. empty :) [18:06] <@qwertydawom> The html form : [18:06] <@qwertydawom> [18:06] <@qwertydawom>
[18:06] <@qwertydawom> Title :
[18:06] <@qwertydawom> URL : [18:06] <@qwertydawom> [18:06] <@qwertydawom>
[18:06] <@qwertydawom> and the php script : [18:06] <@qwertydawom> $title = $_POST['title']; [18:06] <@qwertydawom> $url = $_POST['url']; [18:06] <@qwertydawom> if(empty($title)) [18:06] <@qwertydawom> { [18:06] <@qwertydawom> print("
The 'Title' is empty !
"); [18:06] <@qwertydawom> exit(); [18:06] <@qwertydawom> } [18:06] <@qwertydawom> //let's check the beginning of the URL [18:06] <@qwertydawom> $check_url = strtolower($url); [18:06] <@qwertydawom> $check_url = substr("$check_url", 0, 7); [18:06] <@qwertydawom> //We check the first 7 chars [18:06] <@qwertydawom> if ($check_url!="http://") [18:06] <@qwertydawom> { [18:06] <@qwertydawom> print("The URL must begin with http://"); [18:06] <@qwertydawom> exit(); [18:06] <@qwertydawom> } [18:06] <@qwertydawom> else [18:06] <@qwertydawom> { [18:06] <@qwertydawom> print("$title : $url"); [18:07] <@qwertydawom> } [18:07] <@qwertydawom> ?> [18:07] <@qwertydawom> So, two possible errors : [18:07] <@qwertydawom> 1 - the title is empty (it'll print it) [18:07] <@qwertydawom> 2 - The url must begin with http:// (it'll print it as well) [18:08] <@qwertydawom> Else, if there isn't any errors, it will display the title and the address of the site :) [18:08] <@qwertydawom> E.g. : binaryuniverse: http://binaryuniverse.net [18:09] <@qwertydawom> With this example, we're beginning to use the conditions [18:09] <@qwertydawom> which is very important in any programming language. [18:12] <@qwertydawom> So, we can check anything in the field, but, don't be too rude, users doesn't like when you take them their freedom. ;) [18:13] <@qwertydawom> The most frequent checks are done on the URL and emails, to check if there's an (at) and a (dot) within the entered email. [18:14] <@qwertydawom> The HTML form : [18:14] <@qwertydawom> [18:14] <@qwertydawom>
[18:14] <@qwertydawom> Your email : [18:14] <@qwertydawom> [18:14] <@qwertydawom>
[18:14] <@qwertydawom> And the check.php code : [18:14] <@qwertydawom> $email = $_POST['email']; [18:14] <@qwertydawom> $dot = strpos($email,"."); [18:14] <@qwertydawom> $at = strpos($email,"@"); [18:14] <@qwertydawom> if($dot=='') [18:14] <@qwertydawom> { [18:14] <@qwertydawom> echo "There has to be a dot in your email"; [18:14] <@qwertydawom> } [18:14] <@qwertydawom> elseif($at=='') [18:14] <@qwertydawom> { [18:14] <@qwertydawom> echo "There has to be an '@' in your email"; [18:14] <@qwertydawom> } [18:14] <@qwertydawom> else [18:14] <@qwertydawom> { [18:14] <@qwertydawom> echo "Your email is: '$email'"; [18:14] <@qwertydawom> } [18:14] <@qwertydawom> ?> [18:15] <@qwertydawom> in one of my scripts, there was an error :) can you find it? [18:15] <@qwertydawom> Here : [18:15] <@qwertydawom> [18:15] <@qwertydawom>
[18:15] <@qwertydawom> Surname :
[18:15] <@qwertydawom> Firstname : [18:15] <@qwertydawom> [18:15] <@qwertydawom>
[18:15] <@qwertydawom> $firstname = $_POST['firstname']; [18:15] <@qwertydawom> $name = $_POST['name']; [18:15] <@qwertydawom> print("
Hey $firstname $name
"); [18:15] <@qwertydawom> ?> [18:16] <@qwertydawom> If you read carefully, you should see why it won't work :) [18:16] <@qwertydawom> Come on, show me you're good pupils ;P [18:17] cause we didnt give god money for it to work / pray hard enough [18:17] <@qwertydawom> hahaha [18:17] which one is it? [18:17] i went and grabbed a bite to eat relly quick [18:17] <@qwertydawom> ah ok.. [18:17] k ... [18:17] <@qwertydawom> Well, let me tell it to you then :) [18:17] qwertydawom: it doesn't work because you don't have a space between $name and [18:17] i think [18:17] <@qwertydawom> no, not that ;) [18:17] humm [18:18] surname [18:18] <@qwertydawom> But, it deals with this "$name" variable.. [18:18] <@qwertydawom> yeah mu! :) [18:18] it shoudl say $surname [18:18] ? [18:18] <@qwertydawom> you've got it! [18:18] yay! [18:18] humm? it was a style problem? [18:18] <@qwertydawom> Congrats ;) [18:20] <@qwertydawom> VI- Conditional statements [18:20] <@qwertydawom> To begin with, let's see the most used instructions : [18:21] <@qwertydawom> if, else, elseif, switch, while, for [18:21] <@qwertydawom> == [18:21] <@qwertydawom> != [18:21] <@qwertydawom> < [18:21] <@qwertydawom> > [18:21] <@qwertydawom> <= [18:21] <@qwertydawom> >= [18:21] <@qwertydawom> "and" or "&&" [18:22] <@qwertydawom> "or" or "||" [18:22] <@qwertydawom> To see how if, else and elseif work, let's look at this example :) : [18:22] <@qwertydawom> $qwerty = 512; [18:22] <@qwertydawom> //some conditional statements [18:22] <@qwertydawom> if($qwerty>=0 && $qwerty<500) //1st [18:22] <@qwertydawom> { [18:22] <@qwertydawom> echo $qwerty.' is between 0 and 499'; [18:22] <@qwertydawom> } [18:22] <@qwertydawom> elseif($qwerty>=500 && $qwerty<1000) //2nd [18:22] <@qwertydawom> { [18:22] <@qwertydawom> echo $qwerty.' is between 500 and 999'; [18:22] <@qwertydawom> } [18:22] <@qwertydawom> else //3rd [18:22] <@qwertydawom> { [18:22] <@qwertydawom> echo $qwerty.' is greater than 999'; [18:22] <@qwertydawom> } [18:22] <@qwertydawom> ?> [18:23] <@qwertydawom> Basically, we set a variable as "512", and we check : [18:23] <@qwertydawom> -if it's between 0 and 499 [18:23] <@qwertydawom> -between 500 and 999 [18:23] <@qwertydawom> -or greater than 999 [18:25] <@qwertydawom> Now, here's an other example to understand better, and, that'll allow us to use switch soon :) : [18:25] <@qwertydawom> $jazzy = 'dog'; [18:25] <@qwertydawom> //some conditional statements [18:25] <@qwertydawom> if($jazzy == 'giraffe') [18:25] <@qwertydawom> { [18:25] <@qwertydawom> echo 'jazzy is a giraffe !'; [18:25] <@qwertydawom> } [18:25] <@qwertydawom> elseif($jazzy == 'elephant') [18:25] <@qwertydawom> { [18:25] <@qwertydawom> echo 'jazzy is an elephant'; [18:25] <@qwertydawom> } [18:25] <@qwertydawom> elseif($jazzy == 'mouse') [18:25] <@qwertydawom> { [18:25] <@qwertydawom> echo 'jazzy is a mouse'; [18:25] <@qwertydawom> } [18:25] <@qwertydawom> elseif($jazzy == 'dog') [18:25] <@qwertydawom> { [18:25] <@qwertydawom> echo 'jazzy is a dog'; [18:25] <@qwertydawom> } [18:25] <@qwertydawom> elseif($jazzy == 'cat') [18:25] <@qwertydawom> { [18:25] <@qwertydawom> echo 'jazzy is a cat'; [18:25] <@qwertydawom> } [18:25] <@qwertydawom> else [18:25] <@qwertydawom> { [18:25] <@qwertydawom> echo 'Maybe an hippopotamus? Who knows...'; [18:25] <@qwertydawom> } [18:25] <@qwertydawom> ?> [18:26] <@qwertydawom> Of course, here, it'll print : Jazzy is a dog [18:26] <@qwertydawom> But, like you might have noticed, this structure is a bit too long [18:26] <@qwertydawom> and, not really easy to write [18:26] <@qwertydawom> that's why we'll use "switch" for that [18:27] <@qwertydawom> The following code is exactly the same as the previous one, but, using switch : [18:27] <@qwertydawom> $jazzy = 'dog'; [18:27] <@qwertydawom> switch($jazzy) [18:27] <@qwertydawom> { [18:27] <@qwertydawom> case 'giraffe': [18:27] <@qwertydawom> echo 'jazzy is a giraffe !'; [18:27] <@qwertydawom> break; [18:27] <@qwertydawom> case 'elephant': [18:27] <@qwertydawom> echo 'jazzy is an elephant'; [18:27] <@qwertydawom> break; [18:27] <@qwertydawom> case 'mouse': [18:27] <@qwertydawom> echo 'jazzy is a mouse'; [18:27] <@qwertydawom> break; [18:27] <@qwertydawom> case 'dog': [18:27] <@qwertydawom> echo 'jazzy is a dog'; [18:27] <@qwertydawom> break; [18:27] <@qwertydawom> case 'cat': [18:27] <@qwertydawom> echo 'jazzy is a cat'; [18:27] <@qwertydawom> break; [18:27] <@qwertydawom> default: [18:27] <@qwertydawom> echo 'Maybe an hippopotamus? Who knows...'; [18:27] <@qwertydawom> } [18:27] <@qwertydawom> ?> [18:27] <@qwertydawom> So, you see the structure of a switch : [18:28] <@qwertydawom> switch { case: break; etc. default: } [18:28] <@qwertydawom> And now, the "famous" while loop... [18:29] <@qwertydawom> I say "famous" because when you begin with it, it's usually a bit messy :) [18:29] <@qwertydawom> $qwerty = 6; [18:29] <@qwertydawom> $i = 0; [18:29] <@qwertydawom> //----------[BEGINNING OF THE LOOP]------------ [18:29] <@qwertydawom> while($i != $qwerty) [18:29] <@qwertydawom> { [18:29] <@qwertydawom> echo 'qwerty is different from '.$i.'
'; [18:29] <@qwertydawom> $i++; // $i++ is equivalent to ($i+1) [18:29] <@qwertydawom> } [18:29] <@qwertydawom> //------------[END OF THE LOOP]------------ [18:29] <@qwertydawom> echo 'qwerty is equal to '.$i; [18:29] <@qwertydawom> ?> [18:30] <@qwertydawom> So, in fact, this script will check the value of $qwerty, and, if it's different from the number it's checking, it'll print it :) [18:30] <@qwertydawom> i.e. : qwerty is different from 1 [18:30] <@qwertydawom> ... [18:30] <@qwertydawom> qwerty is different from 5 [18:30] -> -qwertydawom- so what is "i" again? [18:31] <@qwertydawom> Well, $i is usually the variable you use within loops [18:31] <@qwertydawom> So here : [18:31] <@qwertydawom> first of all, it takes the value of 0 ( $i = 0; ) [18:32] <@qwertydawom> Then, while it's different from the value of $qwerty (6 in this case), it'll do the job within the brackets ( while($i != $qwerty) ) [18:33] <@qwertydawom> Then, until it doesn't reach the value, it increases :) ( $i++; ) [18:33] <@qwertydawom> And, eventually, it'll print : qwerty is equal to 6 (in this case) [18:34] <@qwertydawom> Now, we're going to see for loops [18:34] <@qwertydawom> They do the same job as 'while' loops, but, once again, it's only a different syntax [18:35] <@qwertydawom> Instead of declaring the counter before the beginning of the loop ($i = 0;) [18:35] <@qwertydawom> and, every time, increasing it from one unity [18:35] <@qwertydawom> ($i++) [18:35] <@qwertydawom> we do it directly in the loop declaration [18:36] <@qwertydawom> Here's the same code as above using a 'for' loop : [18:36] <@qwertydawom> $qwerty = 6; [18:36] <@qwertydawom> //----------[BEGINNING OF THE LOOP]------------ [18:36] <@qwertydawom> for($i=0; $i != $qwerty ; $i++) [18:36] <@qwertydawom> { [18:36] <@qwertydawom> echo 'qwerty is different from '.$i.'
'; [18:36] <@qwertydawom> } [18:36] <@qwertydawom> //------------[END OF THE LOOP]------------ [18:36] <@qwertydawom> echo 'qwerty is equal to '.$i; [18:36] <@qwertydawom> ?> [18:37] <@qwertydawom> Well, we're done with the loops, and, notice it's really important to know how to use them, because it's widely used! :) [18:37] * qwertydawom sets mode: -m [18:37] well [18:37] Can you use ? : in place of ifelse? [18:37] why can't we do the "while" loop that way? [18:37] because the while can be used in recurison for different things... [18:38] k [18:38] <@qwertydawom> siph0n : yes we can, but I won't explain ternary operator (or, oh well, maybe I should :)) [18:38] :P [18:38] i'm j/w [18:38] sometimes it's easier than ifelse when dealing with numbers [18:38] <@qwertydawom> agreed ;) [18:38] ty Siph0n [18:39] <@qwertydawom> So, the ternary operator is used like this : [18:39] <@qwertydawom> test ? part1 : part2 [18:40] it's used in the same context as C and Perl, right? [18:40] yea [18:40] <@qwertydawom> yes, in C/C++/perl/js [18:40] <@qwertydawom> ex1 ? ex2 : ex3 [18:41] <@qwertydawom> it will return ex2 if ex1 is true [18:41] <@qwertydawom> and ex3 if ex1 is false [18:41] <@qwertydawom> got it? :) [18:41] yes [18:44] <@qwertydawom> VII - File Input/Output [18:45] <@qwertydawom> Now, we'll see how to open and read a file located on your server [18:45] <@qwertydawom> First of all, you create a *.txt file (of course, you can put the extension you want, and, even, none :)) [18:46] <@qwertydawom> So, in our 'data.txt' let's put : 1523 [18:46] <@qwertydawom> And, let's make a php script that'll read data.txt and print what's inside it [18:47] <@qwertydawom> $fp = fopen("data.txt","r"); //(1) [18:47] <@qwertydawom> $datas = fgets($fp,255); //(2) [18:47] <@qwertydawom> fclose($fp); //(3) [18:47] <@qwertydawom> //Prints the result [18:47] <@qwertydawom> echo'In the file there is : '.$datas; [18:47] <@qwertydawom> ?> [18:47] <@qwertydawom> So, you see, it's pretty simple to read from a file [18:48] <@qwertydawom> 1. We open the data.txt only for reading (r) and with the function fopen() [18:48] <@qwertydawom> 2. The reading is being done thanks to the fgets() function [18:49] <@qwertydawom> and we specify the number of chars, here 255, consequently, the first line [18:49] <@qwertydawom> 3. And eventually, we just need to close the file, using the exit() function. :) [18:50] <@qwertydawom> 4. We print what's inside the file using our $datas variable [18:50] <@qwertydawom> Let's get back on the first line [18:51] <@qwertydawom> The 'r' indicates we open the file only for reading [18:51] <@qwertydawom> to open it in reading/writing, we'll use : r+ [18:52] <@qwertydawom> About the fgets() function, here, of course, we could have put "4" instead of "255", because 1523 is only four digits long... :) [18:52] <@qwertydawom> But, putting "255" doesn't cause any problem [18:53] <@qwertydawom> anyway, keep in mind 255 = one line [18:53] <@qwertydawom> so, if you put 256, it'll go to another line [18:53] <@qwertydawom> And, now, ladies and gentlemen (:P), I'll show you a... counter script! :D [18:54] <@qwertydawom> Here, the text file will be called : counter.txt [18:55] <@qwertydawom> $fp = fopen("counter.txt","r+"); // 1.We open the file for reading/writing [18:55] <@qwertydawom> $nbvisits = fgets($fp,11); // 2.We get the number from the file [18:55] <@qwertydawom> $nbvisits++; // 3.We increase the number of visits(+1) [18:55] <@qwertydawom> fseek($fp,0); // 4.We go to the beginning of the file [18:55] <@qwertydawom> fputs($fp,$nbvisits); // 5.We write in the file the new number [18:55] <@qwertydawom> fclose($fp); // 6.We close the file [18:55] <@qwertydawom> print("$nbvisits visitors"); // 7.We print the counter [18:55] <@qwertydawom> ?> [18:56] <@qwertydawom> You just have to place this code in the 'index.php' at your site :) [18:57] <@qwertydawom> To be known : php allows you to create and delete files on a server, so you can search more about that if you like :) [19:12] <@qwertydawom> VIII - Create your own functions [19:12] <@qwertydawom> In php, there a lot of functions, but, an other advantage is, being able to create them yourself :) [19:13] * qwertydawom sets mode: +m [19:13] <@qwertydawom> This will be really useful to avoid typing whole parts of code over and over again. [19:13] <@qwertydawom> Here's the 'function.php' : [19:13] <@qwertydawom> function Arial($size,$color,$texte) [19:13] <@qwertydawom> { [19:13] <@qwertydawom> print("".$texte.""); [19:13] <@qwertydawom> } [19:13] <@qwertydawom> ?> [19:14] <@qwertydawom> And, here's our 'index.php' : [19:15] <@qwertydawom> Require("function.php"); // we call the page containing the function [19:15] <@qwertydawom> // printing ------------------------- [19:15] <@qwertydawom> Arial("2","red","Here\'s the text ..."); [19:15] <@qwertydawom> Arial("3","#0F74A3","The other text ..."); [19:15] <@qwertydawom> ?> [19:15] <@qwertydawom> And, when we'll execute this script, you'll see : [19:16] <@qwertydawom> "Here's the text" printed using : Arial font, its size will be "2" and with a red color [19:17] <@qwertydawom> And : "The other text" -> font = arial, size = 3, color = blueish.. [19:17] <@qwertydawom> So, you see, it's quite easy [19:17] <@qwertydawom> first, you create your function, name it and tell it what to do [19:17] <@qwertydawom> And then, you can call it in your scripts [19:18] <@qwertydawom> Trick : I advice you to create a file (functions.php) with all your functions [19:19] <@qwertydawom> and, then, just put a require() to be able to used those functions [19:19] <@qwertydawom> use* [19:19] <@qwertydawom> I think you see the interest in creating your own functions :) [19:20] whats the difference between Require and Include? [19:21] <@qwertydawom> Well, I think the best answer is given here : http://www.alt-php-faq.org/local/78/ (yeah, I had prepared this question :)) [19:23] so include only includes it if it's usd and require includes it period [19:24] <@qwertydawom> Well, in fact [19:24] <@qwertydawom> require is called before the execution of the script [19:24] ooo [19:25] <@qwertydawom> wheras include can be called, if needed, during the execution of the script [19:25] oh ok [19:25] so require is the equiv of #include [19:25] <@qwertydawom> almost that, yeah :) [19:25] sweet [19:26] <@qwertydawom> and, include will always need a conditional statement [19:26] <@qwertydawom> example : [19:26] <@qwertydawom> ... [19:26] <@qwertydawom> if($value) [19:26] <@qwertydawom> { [19:26] <@qwertydawom> include("scriptA.inc.php"); [19:26] <@qwertydawom> require("scriptB.inc.php"); [19:26] <@qwertydawom> } [19:26] <@qwertydawom> ... [19:26] <@qwertydawom> ?> [19:26] <@qwertydawom> Here, the scriptB will always be included [19:26] <@qwertydawom> But, the scriptA will only be included if the condition is true [19:27] <@qwertydawom> got it? :) [19:27] yep [19:27] <@qwertydawom> cool [19:27] ok [19:27] so [19:27] Require it is :P [19:27] <@qwertydawom> :p [19:28] <@qwertydawom> so so so, now, if you want, we're going to see "environment's variables" :) [19:28] global variables? [19:29] <@qwertydawom> nope [19:29] <@qwertydawom> environment :) [19:30] o [19:30] <@qwertydawom> ok? [19:30] e.g PATH etc [19:30] ah i'm a bit out of it tonight [19:30] <@qwertydawom> lol, well, in fact [19:30] <@qwertydawom> to be honest, environment variables are part of globals' :) [19:31] <@qwertydawom> They belong to the family of : $_SERVER [19:31] i don't understand but i haven't read the link [19:34] <@qwertydawom> IX - Environment variables [19:35] <@qwertydawom> Simple code : [19:35] <@qwertydawom> print("Votre adresse IP est : $_SERVER['REMOTE_ADDR']"); [19:35] <@qwertydawom> ?> [19:35] <@qwertydawom> Shit! [19:35] :O #lecture You need voice (+v) (#lecture) [19:35] <@qwertydawom> Votre adresse IP est = Your IP address is* [19:36] <@qwertydawom> And, now you know how to get the IP address from one of your visitors ;) [19:36] <@qwertydawom> it's the environment variable : $_SERVER['REMOTE_ADDR'] [19:37] <@qwertydawom> an other useful one is : [19:37] <@qwertydawom> HTTP_USER_AGENT [19:37] <@qwertydawom> or : [19:37] <@qwertydawom> HTTP_REFERER [19:38] <@qwertydawom> that's how you can get infos from your visitors :) and/or scare people with 'their' ip in 'your' sig at the forums ;) [19:40] <@qwertydawom> And, now, if you want to know the configuration and the version your server's using, this function will do the trick :) : [19:40] <@qwertydawom> phpinfo() [19:40] <@qwertydawom> phpinfo(); [19:40] <@qwertydawom> ?> [19:40] <@qwertydawom> It will print : [19:40] <@qwertydawom> .. too long to be shown here ;) [19:40] hmm.. [19:41] hold on...imma og make my friend shit his pants XD [19:41] <@qwertydawom> hahaha [19:41] lmao [19:41] <@qwertydawom> alright :) [19:41] with the "your IP is?" [19:41] <@qwertydawom> I think so ;) [19:41] is it's fine :) [19:42] <@qwertydawom> in fact, but, i mean [19:42] <@qwertydawom> (so, to do such a little script, you can use it :)) [19:42] <@qwertydawom> yes mu? [19:42] are there times " no [19:43] so alright [19:43] <@qwertydawom> yes :) [19:43] for all practical purposes [19:43] I think it's a "newer browsers understand we can disregard " if you want [19:43] <@qwertydawom> but, in fact [19:44] <@qwertydawom> the problem with " why? [19:44] yeah [19:44] why? [19:44] <@qwertydawom> because, for example, in xml, the tag is : ohhhh [19:44] so it has to parse more stuff? [19:44] <@qwertydawom> so, when you put your php script with only " it has to check if the code within the tags is php, or xml etc. [19:45] damn dudes [19:45] <@qwertydawom> that's why it's better to use : doesn't work for me...must be something wrong with my php [19:45] <@qwertydawom> hmm [19:45] i told you i'm learning something...even if it is slowly [19:45] print it here Siph0n [19:45] <@qwertydawom> yes :) [19:45] http://siph0n.r8.org/Untitled.php [19:46] no [19:46] <@qwertydawom> but, what's the source? [19:46] [19:46] [19:46] print("Hi, your IP is : $_SERVER['REMOTE_ADDR']"); [19:46] echo 'Thanks for the info!'; [19:46] ?> [19:46] [19:46] [19:46] <@qwertydawom> weird [19:46] <@qwertydawom> it should work.. [19:47] try [19:47] $ip = $_SERVER['REMOTE_ADDR']; [19:47] print("hi ... $ip"); [19:47] <@qwertydawom> yep [19:47] <@qwertydawom> or, maybe with double quotes [19:47] <@qwertydawom> like : [19:48] <@qwertydawom> $_SERVER["REMOTE_ADDR"] [19:54] what verison of php you reccommend? [19:54] 5 or 4? [19:54] recommend* [19:54] <@qwertydawom> Well, I'm using version 4 [19:54] <@qwertydawom> but, 5th one is pretty cool because of the "bc" :) [19:55] <@qwertydawom> it allows you to deals with big numbers [19:55] oh [19:55] yeah *nix calc [19:55] bc is the shit [19:55] dc is a bit odd [19:56] i'll try 5 again [19:56] if it doesn't work [19:56] i'll do 4 [19:56] <@qwertydawom> lol ok [19:56] <@qwertydawom> http://www.phpbuilder.com/board/archive/index.php/t-10298620.html :) [20:00] <@qwertydawom> X - Useful functions [20:01] <@qwertydawom> addslashes() : the name says it all.. it adds backslashes before special chars [20:01] <@qwertydawom> example : $res = addslashes("I'm"); [20:01] <@qwertydawom> result : I\'m [20:02] <@qwertydawom> stripslashes : it does the contrary [20:02] <@qwertydawom> dechex() : returns the hexadecimal value of a number [20:02] <@qwertydawom> example : 1234 -> 4D2 [20:03] <@qwertydawom> ceil() : returns the following integer [20:03] <@qwertydawom> example : $res = ceil("12.1"); [20:03] <@qwertydawom> result : 13 [20:04] <@qwertydawom> (use floor() to get the previous integer and round() for the closest integer) [20:04] <@qwertydawom> chunk_split() : we'll directly see an example : [20:05] <@qwertydawom> $res = chunk_split("DGDFEF","2","-"); [20:05] <@qwertydawom> result : DG-DF-EF- [20:05] <@qwertydawom> htmlentities() : replaces chars with their equivalent in html [20:05] <@qwertydawom> example : $res = htmlentities("&"); [20:05] <@qwertydawom> result : & [20:06] <@qwertydawom> strlen() : returns the length of the string [20:06] <@qwertydawom> strtoupper() : capitalize all the letters (the opposite function is : strtolower()) [20:07] why ceil tho? [20:07] what's it mean? [20:07] ceiling [20:07] =) [20:07] awesome [20:07] No wonder people like php [20:07] <@qwertydawom> :) [20:08] damn it..all languages need this stuff [20:08] <@qwertydawom> hahaha [20:08] and strtoupper? i get the "upper" [20:08] i can't tell you how many times "addslashes()" would've saved my ass in perl.. [20:08] <@qwertydawom> =string to upper case :) [20:08] xD [20:09] <@qwertydawom> i can't tell you how many times "addslashes()" would've saved my ass in perl.. --> hehe, I don't know perl (properly) [20:13] <@qwertydawom> yeah, another trick, when you don't know a function in php : http://www.php.net/thefunctionyoudontknow [20:13] <@qwertydawom> :)