00:00
00:00
Newgrounds Background Image Theme

Matilly just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Php: Advanced Misc

2,850 Views | 8 Replies
New Topic Respond to this Topic

Php: Advanced Misc 2006-06-23 15:42:22


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

Response to Php: Advanced Misc 2006-06-23 15:47:55


very nice tutorial. helped me out a lot. Thanks


Hello, from the past!

[ PHP: Main ]

BBS Signature

Response to Php: Advanced Misc 2006-06-23 15:53:35


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.

Response to Php: Advanced Misc 2006-06-23 15:57:32


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

Response to Php: Advanced Misc 2006-06-27 17:25:00


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);


Merkd.com - It Pays to Play

Earn real money by betting and investing; or, sponsor, challenge, compete,

recruit, communicate, network, earn money playing games, and much more.

Response to Php: Advanced Misc 2006-07-17 12:54:36


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.

Response to Php: Advanced Misc 2006-07-18 00:00:53


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.

Response to Php: Advanced Misc 2006-07-18 07:22:15


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!

Response to Php: Advanced Misc 2006-07-18 11:22:03


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?