Log in

View Full Version : [Tutorial] - PHP ~ The Basics.



Protege
11-03-2008, 05:54 PM
"PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly."PHP is a server-side code, meaning its processed before you even see the page, client-side coding is like javascript, and is processed as/when the page is loaded - Correct me if I'm wrong.

In order to start a PHP code, you have to use the correct syntax (The rules that must be followed to write properly structured code.) There are a few ways to start / end the PHP page, either: (I use the first one.)



<?
?>or


<?php
?>All PHP pages, have to end with the ".php" extension, if not the code won't work. EG: if your page is "index.html" and contains PHP then rename it to "index.php"

Echos
Lets just say, its basically like "Shouting", onto a page... There are different ways to echo


<?
echo("Hello World!");
?>
<?
echo "Hello World";
?>Different people use different methods, but at the end of the day they both do the same principle.

Try making a page called "driftpanzy.php" - Please check if you have PHP installed on your server/hosting or just ask me for 10MB Hosting + 100MB Bandwidth free of charge, TOS applies

Once you've made the file, try doing HTML + PHP, I don't recommend building big systems like this, but you can get the hang of it.



<html>
<head>
<title>PHP DriftPanzy</title>
</head>
<body>
<?
echo("Hello World");
?>
</body>
</html>
It will display:

Hello WorldSemicolons...
Semicolon ends a line of PHP code, signifying the end of a PHP statement and should never be forgotten. EG: If we repeated "Hello World" code several times, then we would need to place semicolons at the end of each statement



<html>
<head>
<title>PHP DriftPanzy</title>
</head>
<body>
<?
echo("Hello World");
echo("Hello World");
echo("Hello World");
echo("Hello World");
?>
</body>
</html>

Hello WorldHello WorldHello WorldHello WorldYou can add "<br>" after the Hello World, before the LAST quotation.

Variables
A variable is a means of storing temporary data, such as a text string "Hello World" or an integer value. A variable can be reused throughout the coding process, to define a variable try the following...




<?
$newvar = "Hello World";
?>You can call $newvar anything, but has to start with a dollar sign ($)

You can also use the echo feature above on it, eg:


<?
$newvar = "Hello World";
echo($newvar);
?>Displays:

Hello WorldAs you can see I didn't include the quotations, i've just placed the var in, if you want to include text/html into the echo you can do this




<?
$newvar = "Hello World";
echo("<br>".$newvar." World is great");
?>Displays:


Hello World World is greatA bit more information on variables

Must start with a letter or underscore "_".
May only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
Variables with more than one word should be separated with underscores. $my_variable
Variables with more than one word can also be distinguished with capitalization. $myVariableYou've covered today: Basic PHP; Echos, showing information on the page; Variables/Strings, how to store temporary data to use later on in the php page.

Any more help just e-mail me! [email protected]
Depending on how much views and how much I help people, I will continue explaining how EASY PHP really is!

Edited by Favourtism (Forum Super Moderator): Thread Closed to avoid constant arguing.

Agnostic Bear
11-03-2008, 06:42 PM
Until you use <?php your code will never be considered "good", <?php works on all servers with php installed while <? does not ;)

Protege
11-03-2008, 06:48 PM
<?php doesn't work on all servers nor does <? - You have to have PHP installed. The latest version of PHP does support it and most hosts usually update their PHP version straight away, But seeing as its just a few letters between being "Good" and "Bad", I must feel so bad not to add them chars. Its not the PHP start tags that make you good, its the logic behind your coding.

Tomm
11-03-2008, 06:50 PM
<?php DOES work on all servers. <? is only works if it is allowed in php.ini


<?php doesn't work on all servers nor does <? - You have to have PHP installed + the latest version of PHP does actually support it - seeing as its just a few more letters = doesn't make you better in any sort of way if you add another extra 3 characters to a line of code.

Protege
11-03-2008, 06:53 PM
So your saying, I will buy a linux box and PHP will just magically work? - Please amuse me? or I will buy a nice windows box, and I will just magically have PHP working... again amuse me. Pointless arguement against a 3 letter addon to the start tags.

Tomm
11-03-2008, 06:57 PM
By all servers I actually ment server with PHP smart *bottom* ''/ I can also be a smart *bottom* as well: Some linux distributions actually do some with PHP installed and ready to use.

Also it is not pointless. Its considered a unoffical standard to use <?php.


So your saying, I will buy a linux box and PHP will just magically work? - Please amuse me? or I will buy a nice windows box, and I will just magically have PHP working... again amuse me. Pointless arguement against a 3 letter addon to the start tags.

Johno
11-03-2008, 07:00 PM
Until you use <?php your code will never be considered "good", <?php works on all servers with php installed while <? does not ;)


<?php doesn't work on all servers nor does <? - You have to have PHP installed. The latest version of PHP does support it and most hosts usually update their PHP version straight away, But seeing as its just a few letters between being "Good" and "Bad", I must feel so bad not to add them chars. Its not the PHP start tags that make you good, its the logic behind your coding.


So your saying, I will buy a linux box and PHP will just magically work? - Please amuse me? or I will buy a nice windows box, and I will just magically have PHP working... again amuse me. Pointless arguement against a 3 letter addon to the start tags.

Now lets see, If you actually read Lolcopters post you would have clearly noticed that he said "<?php works on all servers with php installed while <? does not ;)"

You see that bit, servers with PHP installed (Look, I even coloured it red for you!) Obviously PHP wont just magically work, It has to be installed.

Please do a little bit of reading of what has actually been said before you try talking down to people such as Tomm and other people who know what they are talking about.

.:; Johno

Navicat
11-03-2008, 07:11 PM
PHP is a server-side code, meaning its processed before you even see the page, client-side coding is like javascript, and is processed as/when the page is loaded - Correct me if I'm wrong.

In order to start a PHP code, you have to use the correct syntax (The rules that must be followed to write properly structured code.) There are a few ways to start / end the PHP page, either: (I use the first one.)



<?
?>or


<?php
?>All PHP pages, have to end with the ".php" extension, if not the code won't work. EG: if your page is "index.html" and contains PHP then rename it to "index.php"

Echos
Lets just say, its basically like "Shouting", onto a page... There are different ways to echo


<?
echo("Hello World!");
?>
<?
echo "Hello World";
?>Different people use different methods, but at the end of the day they both do the same principle.

Try making a page called "driftpanzy.php" - Please check if you have PHP installed on your server/hosting or just ask me for 10MB Hosting + 100MB Bandwidth free of charge, TOS applies

Once you've made the file, try doing HTML + PHP, I don't recommend building big systems like this, but you can get the hang of it.



<html>
<head>
<title>PHP DriftPanzy</title>
</head>
<body>
<?
echo("Hello World");
?>
</body>
</html>
It will display:
Semicolons...
Semicolon ends a line of PHP code, signifying the end of a PHP statement and should never be forgotten. EG: If we repeated "Hello World" code several times, then we would need to place semicolons at the end of each statement



<html>
<head>
<title>PHP DriftPanzy</title>
</head>
<body>
<?
echo("Hello World");
echo("Hello World");
echo("Hello World");
echo("Hello World");
?>
</body>
</html>
You can add "<br>" after the Hello World, before the LAST quotation.

Variables
A variable is a means of storing temporary data, such as a text string "Hello World" or an integer value. A variable can be reused throughout the coding process, to define a variable try the following...




<?
$newvar = "Hello World";
?>You can call $newvar anything, but has to start with a dollar sign ($)

You can also use the echo feature above on it, eg:


<?
$newvar = "Hello World";
echo($newvar);
?>Displays:
As you can see I didn't include the quotations, i've just placed the var in, if you want to include text/html into the echo you can do this




<?
$newvar = "Hello World";
echo("<br>".$newvar." World is great");
?>Displays:
A bit more information on variables
Must start with a letter or underscore "_".
May only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
Variables with more than one word should be separated with underscores. $my_variable
Variables with more than one word can also be distinguished with capitalization. $myVariableYou've covered today: Basic PHP; Echos, showing information on the page; Variables/Strings, how to store temporary data to use later on in the php page.

Any more help just e-mail me! [email protected]
Depending on how much views and how much I help people, I will continue explaining how EASY PHP really is!

That is awful structured code, its ugly.

Plus, you shouldn't ever teach users to use short tags, <? instead of <?php or <%.

Quit using ('s. You don't need them most of the time. You can easily do:



<?php
echo $variable;
?>


Without them.


You don't need a semicolon at the end of everything, or every statement..

example:



<?php
if( $variable == "Something") {
echo "Hello" . $variable;
}
?>



You use brackets, and lots of other things.

You do not have to have the extension .php, my site uses .do and .ko also .php4, .php3, .php2, .php5, .phps

Protege
11-03-2008, 07:14 PM
If you want to be a little **** and try be a sarcastic ****, I will treat you like a little **** "all servers" quoted by your lovely friend "Lolcopters", I stated that it wouldn't be on all servers and gave an example.
Difference between <? and <?php ? - both start/end tags for a PHP page and one has 3 extra characters signifying the language, I'm so sincerely sorry for "Talking down" to people such as "Tomm" as I honor them in all their glory and suck up to them like a mouth to a Pepsi bottle, as they know what they are talking about and I clearly don't know at all, I clearly attempted to teach people a little PHP but you wish to start a confrontation over a little bit of coding differentiation, but seeing as I'm trying to tell the forum a easier way to learn PHP for the people who can't already get their heads round it and then get told I'm wrong in all ways, I never asked for criticisms and I never said which way was right, I said "(I use the first one.)" I never told you guys to use it, I just said "(I use the first one.)" So maybe read before you talk as "ActiveVision" said. I accept comments from people who know what they are saying, not suck ups and sarcastic screw heads. No criticisms to anyone else but ActiveVision

Protege
11-03-2008, 07:16 PM
I said statements not "if statements" or "while statements", short tags can be taught to anyone, theres different ways people do things and thats how I do it, and it works at the end of the day, I'm not teaching people who already know I'm teaching people who want to know.

Edited by Favourtism (Forum Super Moderator): Please do not double post.

Navicat
11-03-2008, 07:16 PM
I don't have the time to read your ******, unorganized post.

Learn to take criticism, we are pointing out something that you don't know, or seem to "think you know."

Short tags (<? <%) must be enabled in PHP.ini, and I believe it is not a default setting.

If the server you work on allows it, your lucky, as most servers do not.

<?php is the official way, better way, and more professional way to do it.

If your coding looks like this:

<?php
$name = 'name';
if($name == "name") { echo "blabla"; } echo $name; if($name == "hello") { echo "hey"; }
?>

Thats not professional at all, thats awful stinky code.



I'm not teaching people who already know I'm teaching people who want to know.

Why not teach them the right way then? Quit trying to be hard headed, your in a group of people who know 300x more about PHP then you ever will.

Protege
11-03-2008, 07:24 PM
What do they know that I don't know? Tell me clever boy, as you seem to know that everything I do is awful and seeing as I don't even code like that above I don't see what your problem is.

http://www.habboxforum.com/showthread.php?t=463370 - Theres an example of me helping someone with a positive outcome, eventually.

Johno
11-03-2008, 07:28 PM
Hey,

If you are going to moan and throw your toys out the pram because someone is giving you criticism and you cant accept it then you would be better off leaving now.

My good friend Lolcopters? I don't even know the guy, same with Tomm. I know they are respected members of the community who actually know a thing or two, so don't bother calling me a suck-up.

At least if you are going to give me a hissy-fit to laugh at, please split up the text and use some punctuation! I mean:


Difference between <? and <?php ? - both start/end tags for a PHP page and one has 3 extra characters signifying the language, I'm so sincerely sorry for "Talking down" to people such as "Tomm" as I honor them in all their glory and suck up to them like a mouth to a Pepsi bottle, as they know what they are talking about and I clearly don't know at all, I clearly attempted to teach people a little PHP but you wish to start a confrontation over a little bit of coding differentiation, but seeing as I'm trying to tell the forum a easier way to learn PHP for the people who can't already get their heads round it and then get told I'm wrong in all ways, I never asked for criticisms and I never said which way was right,

Thats one big ol' sentence you have there! Look at all the wonderful commas! Truly a master of the English language.

.:; Johno


If you want to be a little **** and try be a sarcastic ****, I will treat you like a little **** "all servers" quoted by your lovely friend "Lolcopters", I stated that it wouldn't be on all servers and gave an example.
Difference between <? and <?php ? - both start/end tags for a PHP page and one has 3 extra characters signifying the language, I'm so sincerely sorry for "Talking down" to people such as "Tomm" as I honor them in all their glory and suck up to them like a mouth to a Pepsi bottle, as they know what they are talking about and I clearly don't know at all, I clearly attempted to teach people a little PHP but you wish to start a confrontation over a little bit of coding differentiation, but seeing as I'm trying to tell the forum a easier way to learn PHP for the people who can't already get their heads round it and then get told I'm wrong in all ways, I never asked for criticisms and I never said which way was right, I said "(I use the first one.)" I never told you guys to use it, I just said "(I use the first one.)" So maybe read before you talk as "ActiveVision" said. I accept comments from people who know what they are saying, not suck ups and sarcastic screw heads. No criticisms to anyone else but ActiveVision

Protege
11-03-2008, 07:34 PM
LOL at the end of the day, I didn't split it up for a reason as I was talking what I was saying you ****** and splitting it with commas for pauses, does this say "Tutorial on how to write a essay" Sorry, im not a nerdy kid like yourself but I hope your laughter paid off in your self defaced life as a necrophiliac ****. Just don't hack me pro boy who uses "BOLD FONT" to get his point across, thats just a sad argument to put across.

php.net
11-03-2008, 07:49 PM
LOL at the end of the day, I didn't split it up for a reason as I was talking what I was saying you ****** and splitting it with commas for pauses, does this say "Tutorial on how to write a essay" Sorry, im not a nerdy kid like yourself but I hope your laughter paid off in your self defaced life as a necrophiliac ****. Just don't hack me pro boy who uses "BOLD FONT" to get his point across, thats just a sad argument to put across.

You're pathetic.

Edited by Favourtism (Forum Super Moderator): Please do not be rude

Johno
11-03-2008, 07:54 PM
Hey,

Yes, I am truly nerdy because I talk properly and type properly as well?

At least when I am having a discussion on a forum I don't feel the need to swear and throw pointless insults like "defaced life as a necrophiliac."

"Just don't hack me pro boy who uses BOLD FONT" - I mean, What the hell? I use bold Verdana at size one as I like the way it looks. Does it really make me seem pro? Wow, you really learn something new everyday.

You won't get anywhere at life until you learn to accept that other people will be better than you. I am not saying that I know everything about coding, but I know there are members of the board who know how to code properly unlike you.

.:; Johno



LOL at the end of the day, I didn't split it up for a reason as I was talking what I was saying you ****** and splitting it with commas for pauses, does this say "Tutorial on how to write a essay" Sorry, im not a nerdy kid like yourself but I hope your laughter paid off in your self defaced life as a necrophiliac ****. Just don't hack me pro boy who uses "BOLD FONT" to get his point across, thats just a sad argument to put across.

Protege
11-03-2008, 08:09 PM
"Just don't hack me pro boy who uses "BOLD FONT" to get his point across, thats just a sad argument to put across." I was doing what you do, and insult my grammar against something besides the point. LOL Ultimate irony but which members have insulted my coding skills? the short-tags aren't a coding skill its a different method to work with, no one has insulted me yet, so please feel free to keep babbling on, its quite amusing in my eyes to be totally honest.

Johno
11-03-2008, 08:13 PM
Hey,

I did not state that other members have "insulted your coding skills" - I stated that "some members of the board know how to code properly unlike you" and I also stated that your insult was nothing short of childish.

.:; Johno


LOL Ultimate irony but which members have insulted my coding skills? the short-tags aren't a coding skill its a different method to work with, no one has insulted me yet, so please feel free to keep babbling on, its quite amusing in my eyes to be totally honest.

Navicat
11-03-2008, 08:14 PM
What do they know that I don't know? Tell me clever boy, as you seem to know that everything I do is awful and seeing as I don't even code like that above I don't see what your problem is.

http://www.habboxforum.com/showthread.php?t=463370 - Theres an example of me helping someone with a positive outcome, eventually.

The coding in that is horrible, simply horrible.


You're pathetic.

He very much is.


"Just don't hack me pro boy who uses "BOLD FONT" to get his point across, thats just a sad argument to put across." I was doing what you do, and insult my grammar against something besides the point. LOL Ultimate irony but which members have insulted my coding skills? the short-tags aren't a coding skill its a different method to work with, no one has insulted me yet, so please feel free to keep babbling on, its quite amusing in my eyes to be totally honest.

Short-tags are a coding skill, its not a different method, its the childish, unprofessional way of coding. Thats it.

PHP6.0 removes short-tags ;) good luck.

Protege
11-03-2008, 08:26 PM
<? = short tag
<?php = "professional way"

Err, whats the difference? Does it make the code run faster? Does it make it look better? Does it actually even effect anything other than the char count?

Its just a coding preference - it makes no difference in the outcome/performance of the script, If anything it increases coding time (If admittedly by a fraction of a second) But its all personal coding preference, since it makes absolutely no difference on whether you do or DON'T use it then you should stop trying to find faults in other peoples coding. If its not your personal preference - Live with it.

Navicat
11-03-2008, 08:30 PM
Actually it does make it run faster, time it ;) It saves PHP the time in checking to see if it is enabled.

Actually, if you use professional IDE software, it will put the <?php ?> for you ;)

Actually it does make a difference if you use it or not ;)

I can, and will, find faults in other people's coding, just like I have done on this forum for years.

If you cannot take criticism, get the hell away from D&D, because that all it is.

myke
11-03-2008, 08:43 PM
Thanks, this will definitely get me started :)

Well done, +REP (if I can)

Invent
11-03-2008, 08:46 PM
<? = short tag
<?php = "professional way"

Err, whats the difference? Does it make the code run faster? Does it make it look better? Does it actually even effect anything other than the char count?

Its just a coding preference - it makes no difference in the outcome/performance of the script, If anything it increases coding time (If admittedly by a fraction of a second) But its all personal coding preference, since it makes absolutely no difference on whether you do or DON'T use it then you should stop trying to find faults in other peoples coding. If its not your personal preference - Live with it.

Short tags are not enabled by default and so if you use them on a commercial script, your script will most probably not work on your customer's server/website.

Protege
11-03-2008, 08:47 PM
So now we're talking about milliseconds; when PHP is processed it automatically goes through the php.ini to check no matter what, and checking a var if it’s true/false isn't going to change the process time.

I don't use "IDE SOFTWARE," I use a notepad ++; It doesn't make a difference; it’s just another way to code in PHP, its like saying “#” or “//” comments aren't professional but “/* */” comments are.

You’re arguing on material that isn't really predictable, I can take criticism as I said in my previous posts but not suck ups, and certainly not pathetic people who attempt to argue over a little "misfit" because their college Tutor didn't teach them it. There are lots of ways of doing things; it relies on your logic and knowledge of different functions in PHP.

Navicat
11-03-2008, 08:50 PM
Excuse me?

Me, a suck up? Who am I going to suck up to? I am one of the best coders here, by far better then you. I'm not saying I am the best, but one of them ;)

I don't need a college "tutor" because I didn't go to college, I am currently in University double majoring in both compuer science and business. Probably farther then you will ever get with your pathetic attitude.

Hypertext
11-03-2008, 08:52 PM
It was extremely basic, a bit like introducing typing in a html lesson, but well done anyway!

Navicat
11-03-2008, 08:54 PM
It was extremely basic, a bit like introducing typing in a html lesson, but well done anyway!
Oh no, don't give him an idea to do that.

Protege
11-03-2008, 09:06 PM
It was extremely basic, a bit like introducing typing in a html lesson, but well done anyway!
Thank you, it was basic but I guess I shouldn't of even bothered.


Oh no, don't give him an idea to do that.
Don't worry, your saved you didn't even have to read this thread.

Excuse me?

Me, a suck up? Who am I going to suck up to? I am one of the best coders here, by far better then you. I'm not saying I am the best, but one of them ;)

I don't need a college "tutor" because I didn't go to college, I am currently in University double majoring in both compuer science and business. Probably farther then you will ever get with your pathetic attitude.
Did I actually point out who was sucking up? I didn't need to go to College nor University to get the knowledge of 3 coding languages before I even reached 18; I learnt them myself, it was that easy. The worst thing about it is you actually needed someone to teach you the knowledge that I already know. I don’t need computing for the career path I have chosen, but I would love to see how you get on because you will need English grammar and spelling skills for the job you’re aspiring to; but, unfortunately you’re failing in that sector.

Invent
11-03-2008, 09:08 PM
Did I actually point out who was sucking up? I didn't need to go to College nor University to get the knowledge of 3 coding languages before I even reached 18; I learnt them myself, it was that easy. The worst thing about it is you actually needed someone to teach you the knowledge that I already know. I don’t need computing for the career path I have chose, but I would love to see how you get on because you will need English grammar and spelling skills for the job you’re aspiring to; but, unfortunately you’re failing in that sector.

Your "tutorial" and coding advice gives off the impression and shows that you don't know the basics of one of the most popular and ever-growing scripting languages at the moment (PHP).

Navicat
11-03-2008, 09:09 PM
Ask anyone on this forum, I taught myself, I went to university for the little slips of paper that you get when you graduate. That matter.

I have English and Grammar skills, unlike you, and how am I failing in that "sector", quit trying to use big words, you are failing at it.

php.net
11-03-2008, 10:19 PM
The fact that you've reached 18 and you've not yet been shot, proves that you don't go out.

Insedated
11-03-2008, 10:20 PM
The fact that you've reached 18 and you've not yet been shot, proves that you don't go out.


Lmao!!!

Edited by Favourtism (Forum Super Moderator): Please do not post short or pointless messages.

Navicat
11-03-2008, 10:21 PM
Who, me?

Insedated
11-03-2008, 10:22 PM
Who, me?

Nah, DriftFrenzy I think.

Navicat
11-03-2008, 10:24 PM
Ahh okay, was about to say :P Usually me and Scott get along.

Protege
12-03-2008, 05:07 AM
and again, you should take advice from your fellow forum users, I never said I was 18, read
idiot.

Edited by Favourtism (Forum Super Moderator): Please do not be rude.

chrisgocrazyH
12-03-2008, 09:52 AM
*Text Removed*

Edited by Favourtism (Forum Super Moderator): Please do not avoid the filter.

Navicat
12-03-2008, 11:29 AM
Your pathetic, you don't even know PHP.

Edited by Favourtism (Forum Super Moderator): Please do not be rude.

Protege
12-03-2008, 02:56 PM
At least some see where I'm coming from
@ Navicat: You must feel so proud as you know PHP, it's not the ONLY language...

Agnostic Bear
12-03-2008, 04:32 PM
At least some see where I'm coming from
@ Navicat: You must feel so proud as you know PHP, it's not the ONLY language...



<?php
class shutupandgetout
{

public $what;
public $do;

function __construct()
{
$this->who = "You";
$this->do = "get out";
$this->what( $this->who, $this->do );
}

function what( $who = "", $do = "" )
{
try
{
$arr = array( $who => "You", $do => "leave" );
$who = ( $who == "" ) ? $arr[$who] : $who;
$do = ( $do == "" ) ? $arr[$do] : $do;
echo( "I think " . $who . " should " . $do );
}
catch( Exception $exception )
{
echo( "What? " . $exception->getMessage() );

}
}

function __destruct()
{
unset( $this->who );
unset( $this->do );
exit( "<br />BOOM" );
}
}
$getout = new shutupandgetout;
?>


wat

Florx
12-03-2008, 04:58 PM
<?php
class shutupandgetout
{

public $what;
public $do;

function __construct()
{
$this->who = "You";
$this->do = "get out";
$this->what( $this->who, $this->do );
}

function what( $who = "", $do = "" )
{
try
{
$arr = array( $who => "You", $do => "leave" );
$who = ( $who == "" ) ? $arr[$who] : $who;
$do = ( $do == "" ) ? $arr[$do] : $do;
echo( "I think " . $who . " should " . $do );
}
catch( Exception $exception )
{
echo( "What? " . $exception->getMessage() );

}
}

function __destruct()
{
unset( $this->who );
unset( $this->do );
exit( "<br />BOOM" );
}
}
$getout = new shutupandgetout;
?>


wat
Nice class.

@DriftPanzy - A word of advice: Don't argue with Scott Diamond you will not win, not now, not in a million years.

Want to hide these adverts? Register an account for free!