Welcome!
Who Can Read This / What shall I learn in this Tutorial?
Anybody can read this, but this Tutorial is actually for those who are getting better with PHP and want to know some good and useful Tips & Tricks.
Let's get started ...
Classes Introduction
Classes are initially introduced in PHP4, but they still lacked.
In PHP5, Classes began to be awesome, like on the other languages (e.g. C familly).
There were introduced statements like implements, visibility (hidden, protected, private), static, abstractions, overloading and other magic functions
introduced in PHP5.
However, in this Tutorial you shall learn just basics of Classes.
So we won't be even touching statements like extends, parent, magic functions (__sleep and __wakeup), $this and so forth.
The class is started with a keyword class. After that, it goes the class name, without two braces [ () ] like on functions.
Programmers often put those.
And we will put some functions onto that class.
It would be expressed thus:
class [name] {
function [name]() {
[stuff]
}
}
class ng {
function name() {
echo "NewGrounds";
}
function slogan() {
echo " - Everything, By Everyone!";
}
}
So, that would basically output 'NewGrounds - Everything By Everyone!' ... somehow....
Now you shall learn how to output data from classes!
You access functions in the class from its 'instance'.
Via instance, you can access functions in the class very easely.
It would be expresed thus:
$[varname] = new [class name]
or:
a variable = new a class you want to create
The word 'new' is the key in the creation of classes. Then, you can access that class via the variable you choosed.
e.g.
$ngStuff = new ng;
And that would create the instance of the class 'ng' we created before.
Now, when we have the instance, we access the functions inside like so:
(echo) $[var name]->name();
Now the key is '->'.
Firstly we put a variable we choosed before to create an instance, and after that the function we want to access.
So, we would access the function 'name()' like so:
echo $ngStuff->name();
Now let's definitly print our class 'ng' out:
<?
$ngStuff = new ng;
echo $ngStuff->name();
echo $ngStuff->slogan();
// Outputes:
// NewGrounds - Everything, By Everyone!
?>
String Replacements and Upper capitalizing words
In this small sub-tutorial I will learn you a good combination: str_replace and ucwords.
Let's create a string. It is a ugly, barely readable one.
<? $str = "my_dog_is_lazy_and_so_FRIKin_uGlY!!it_sme
lls_REALLY_REALLY_BaD!!"; ?>
You sometimes might get something like that in your input.
So, let's introduce the str_replace firstly:
str_replace(search for, replace found, string);
We would seek for for '_' (underscores) and replace them with whitespaces (' ').
It is really easy to do it with str_replace. I will show you how now:
<?
$str = $str; // we have already defined it.
$str = str_replace('_', ' ', $str);
?>
The $str is now:
"my dog is lazy and so FRIKin uGLY!!it smells REALLY REALLY BaD!!";
Much better, but still no so good right?
Let me introduce you ucwords:
ucwords(string);
Ucwords does the upper capitalizing of any first word after the whitespace.
Let's finally covert the $str:
<?
$str = $str;
// but firstly we will lower all the caps
$str = strtolower($str);
// that is an easy function that needs to explanation
$str = ucwords($str);
?>
The $str is now:
"My Dog Is Lazy And So Frikin Ugly!!It Smells Really Really Bad!!";
It looks much more professional now, HOWEVER, there is now too much of upper capitalized words.
It is grammatically bad nonetheless.
So, we could see the ucfirst too. Ucfirst is actually the same as ucwords, except for one thing - ucfirst will convert just the first letter of string.
No good right?
That's why we will firstly implement a explode / implode function?
note: you should already know that. if you don't, please check out the explode / implode Tutorial. find it at the PHP:Main thread pointed at the beggining.
<?
$str = explode('!!', $str);
$str1 = ucfirst($str[0]);
$str2 = ucfirst($str[1]);
$str = $str1 . "!! " .$str2 . "!!";
echo $str;
?>
The result is:
"My dog is lazy and so frikin ugly!! It smells really really bad!!";
Good huh?
Formatting Code In PHP / Making It Readable
There are several special combinations in PHP previousled with a backslash ('\').
Furthermore, those need to be in the double quotes (' " ').
The most important is '\n' and the second one is '\t'.
\n is used for going to the new lines.
\t is used for emulating the tabs, regardless in which way (left or right).
We are talking about formatting a code which will be shown when you look at the Page Code in your Browser.
Let's example this:
<?
echo "The summer have began! <br />";
echo "What shall you do?? <br />";
echo "<br /> I will probably nothing.";
echo "<br />";
echo "lolz, bie!";
?>
That would output:
The summer have began!
Whall shall you do??
I will probably nothing.
lolz, bie!
That's okay, but the code will NOT be the same.
The code will be:
(everything is in the same line)
The summer have began! <br /> What shall you do?? <br /> <br /> I will probably nothing. <br /> lolz, bie!
Let's change that.
note: using \t makes no sense now, but it is generally used for tables and similar.
<?
echo "The summer have began! \n <br /> \n";
echo "\t What shall you do?? \n <br /> \n";
echo "\t <br /> \n \t I will probably nothing. \n";
echo "\t <br /> \n";
echo "lolz, bie! \n";
?>
Output will be the same, however, the code won't be.
The code will be:
The summer have began!
<br />
......What shall you do??
<br />
......<br />
......I will probably nothing.
......<br />
lolz, bie!
And that's actually it.
Nino
nino AT recgr DOT com
http://www.recgr.com