Be a Supporter!
NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Php: Advanced Misc Jun. 23rd, 2006 @ 03:42 PM Reply

Welcome!

PHP: Main

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

Momo-the-Monkey
Momo-the-Monkey
  • Member since: Oct. 15, 2005
  • Offline.
Forum Stats
Member
Level 45
Musician
Response to Php: Advanced Misc Jun. 23rd, 2006 @ 03:47 PM Reply

very nice tutorial. helped me out a lot. Thanks


Hello, from the past!
[ PHP: Main ]

BBS Signature
NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Advanced Misc Jun. 23rd, 2006 @ 03:53 PM Reply

At 6/23/06 03:47 PM, Momo_the_Monkey wrote: very nice tutorial. helped me out a lot. Thanks

No problems, it was my pleasure.

eWhabs
eWhabs
  • Member since: May. 4, 2006
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to Php: Advanced Misc Jun. 23rd, 2006 @ 03:57 PM Reply

Holy yeah!

That'll sure be usefull to me *when I get better at PHP*


-Disregard females (fuck bitches)
-Acquire currency (get money)
-Ignite cannabis frequently (smoke sum with your homies)

BBS Signature
Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to Php: Advanced Misc Jun. 27th, 2006 @ 05:25 PM Reply

Good tutorial, but I would use a preg_split on your ucfirst() example. Let's say they used a period, or a question mark. You could split it like this:

preg_split('#(\.|!\/?)#i', $str);

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Advanced Misc Jul. 17th, 2006 @ 12:54 PM Reply

At 6/27/06 05:25 PM, Pilot-Doofy wrote: Good tutorial, but I would use a preg_split on your ucfirst() example. Let's say they used a period, or a question mark. You could split it like this:

preg_split('#(\.|!\/?)#i', $str);

I was trying to learn bits of regular expressions for hours...and failed.

Shorter: I haven't learnt regular expressions (yet).

However, in your case, you are talking about a few cases. Period, question mark?

$str = str_replace('!', '! ', $str);
$str = str_replace('?', '? ', '$str);

You are using just one small line, but str_replace is faster than preg_split.

Craige
Craige
  • Member since: Jul. 17, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Php: Advanced Misc Jul. 18th, 2006 @ 12:00 AM Reply

Okay, I only read to the end of the OOP part, and I just have to say, what? Frist off, why would you group OOP in with misc.? It is a huge part of PHP; it deservs it's own topic.

Seccond of all, if you are writing a OOP tutorial, write it on PHP 5 OOP. That means visibility and the whole deal. People are getting left in the days of PHP 4, and are going to be stunned when PHP 6 arrives. So teach them PHP 5 OOP first, and later mention the downfalls of PHP 4 OOP, otherwords, they will never advance either.

Not to be rude, but I feel people should have a good first introduction to OOP first, to ensure they will not have struggles wtih it in a few weeks and give up.

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to Php: Advanced Misc Jul. 18th, 2006 @ 07:22 AM Reply

At 7/18/06 12:00 AM, Craige wrote: Okay, I only read to the end of the OOP part, and I just have to say, what? Frist off, why would you group OOP in with misc.? It is a huge part of PHP; it deservs it's own topic.

But that one is already done!

Craige
Craige
  • Member since: Jul. 17, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Php: Advanced Misc Jul. 18th, 2006 @ 11:22 AM Reply

At 7/18/06 07:22 AM, Nino_JoJ wrote: But that one is already done!

I didn't see it in the list, and I don't remember reading it before either. I may be wrong, but I don't think it has been done. And even if it has, why would you re-introduce it here?