Vault9 Modding Vault9 Graphics Vault9 Code Vault9 Net Vault9 OS Vault9 Gaming Vault9 ChillZone Vault9 Tech Vault9 Archives       Vault9 Global9       Vault9 Fusion     Vault9 Blog     Vault9 Network          
 

Please Note!

This is a read-only board, no new topics or registrations are allowed. The Code Vault has moved to http://forums.vault9.net - Be sure to find more information there!


 
Reply to this topicStart new topic
> Php, Where To Start
post Jun 18 2004, 04:22 AM
Post #1


bovine satisfaction
Group Icon

Group: Administration
Posts: 1,748
Joined: 27-April 02
From: In teh pastures
Member No.: 162

Sex: Male



So you want to start programming in PHP, here's a couple of starters and links that can help you on your way...

As with every programming language, the "hello world" example is used as the first script. This tutorial will be no different ":D

To display items, we will use the "echo" function, there is also the "print" function, but i prefer echo ":D

so...
Hello World
CODE
<?php
echo("Hello World\n");
?>


Your first php page is now complete! You can do that in notepad if you want, as long as you have the php backend installed, it will be processed and displayed. Just remember to save it with the .php extension.

You might be wondering what the \n does - it's a break, which says that a new line must be used. Also, the ; is placed at the end of each line, otherwise you will get errors along the lines of...

QUOTE("Error")
Parse error: parse error, unexpected T_VARIABLE in f:\work\webpages\script.php on line 15


Now to get a bit advanced...
We going to start using variables...

The Variable

CODE
<?php
$var = "Hello";
$var2 = "World";
echo("$var $var2 \n");
?>


Now you see a period (.) - A . in php joins things. So if i was to say -
$var = "Hello";
$var .= " World";
when echo'ing $var, you'd get the entire "Hello World" effect.

Also, you may see that everything being echo'ed is encased in "'s, this is so that the value of the variable is given and not just the word $var. For example...

CODE
<?php
$var = "ROAR";
echo("I like to $var<br>\n");
echo('I like to $var');
?>

This will output the following...
QUOTE
I like to roar
I like to $var


You see that i have used an html tag in there, with php you can use all the standard html tags, seeing as the page is displayed in html format (just has a .php extension). <br><b><u><font> etc. all those can be used.
When using a URL in a string, one must be careful seeing as it is an html tag that uses "'s to get the value itself. This problem is solved in three ways...

The Solution

Double quotes within single quotes
CODE
<?php
$url = '<a href="www.yoursite.com" target="_blank"></a>';
echo("$url\n");


Variables using single quotes
CODE
<?php
$var = 'MOO!';
$link = '<a href="www.somesite.com">' . $var . '</a>';
echo("$link\n");
?>


Using escape characters
CODE
<?php
echo("<a href=\"www.yoursite.com\" target=\"_blank\"></a>\n");
?>


So now you have some basic knowledge on setting variables, and displaying them, as well as getting around some obstacles.

The only thing with using the single quote, some of the escape characters such as \n will not be executed, but rather displayed, in this case you would have to use the <br> alone

How's about using conditions to determine what will happen?

The If

If you have any other programming experience, you know what the if is about. It basically checks to see if what you are saying suits the condition. The condition can also be manipulated into a false (If not etc.). There is a right, and a wrong way to format the if statement, not only does it make it easier to see what you have done, it also makes the code better looking. The code below is formatted in the generally accepted manner.

CODE
<?php
$var = 1;

if($var == 1){
echo("There is a match, the statement matches the condition");
}
else{
echo('There is no match, the statement does not match the condition of $var<br>');
};


Following the if is, the compound if statement, also known as the case statement.

The Case Statement

What this does is save you from having to right 39 if statements using the same variable in the condition.

Case vs. If Example
CODE
<?php
   // Seed random number generator
   srand((double)microtime() * 1000000);

   // Pick a random lowercase letter and announce it
   $letter = chr(rand(97, 122));
   echo("The letter is '" .  $letter . "'<br>");

   echo("Single if: ");
   if($letter == "a" || $letter == "e" || $letter == "i" ||
      $letter == "o" || $letter == "u"){
    echo("The letter is a vowel!<br>");
}
   else {
 echo("The letter is NOT a vowel!<br>");
}


   echo("If-elseif-else: ");
   if($letter == "a") {
 echo("The letter is a vowel!<br>");
}
   else if($letter == "e") {
 echo("The letter is a vowel!<br>");
}
   else if($letter == "i") {
 echo("The letter is a vowel!<br>");
}
   else if($letter == "o") {
 echo("The letter is a vowel!<br>");
}
   else if($letter == "u") {
 echo("The letter is a vowel!<br>");
}
   else {
 echo("The letter is NOT a vowel!<br>");
}

   echo("Switch: ");
   switch($letter)
   {
        case "a":
        case "e":
        case "i":
        case "o":
        case "u":
         // Notice if $letter is not 'u', it 'falls' through to here
          echo("The letter is a vowel!<br>");
          break;
        default:
          echo("The letter is NOT a vowel!<br>");
          break;
   }
?>


Until now, we have been using native functions, but sometimes there is not a funciton designed for what you need. So you need to code your own.

The Function

As with everything, there is a proper way to call a function.
The right way --> $var = calcadd($n1, $n2);
The wrong way --> $var = calcadd($n1,$n2);

now to code the function...
CODE
<?php

$n1 = 4;
$n2 = 7;

function calcadd($n1, $n2){
 $n1 = $n1 + $n2;
 return $n1;
};

$call = calcadd($n1,$n2);
echo("$call");

?>

This is a very basic tutorial that should allow you to start somewhere and gain experience.

Here are some helpful links...

1
2
3

I borrowed the format of this tutorial from link 3, but i changed the content incase you noticed a similarity

This post has been edited by ::DocterMoo::: Jun 18 2004, 04:37 AM


--------------------
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post Jun 18 2004, 04:22 AM
Post #


Member


Group: Administration

Joined: Today, 11:57 PM





Quote PostGo to the top of the page
post Jun 18 2004, 06:12 AM
Post #2


~Pulsage~
Group Icon

Group: Regular Member
Posts: 551
Joined: 1-May 02
From: South Africa - Gauteng
Member No.: 179

Sex: Male



a nice thing I learned when I started ...

instead of doing like so :

CODE

This is my webpage and today is <?php echo date("Y-m-d"); ?>.


do like so :
CODE

This is my webpage and today is <?=date("Y-m-d");?>


you won't believe what code I've come across... this save alot of time, although now it may not seem so ...


--------------------

IPB Image
IPB Image
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post Jun 18 2004, 06:30 AM
Post #3


bovine satisfaction
Group Icon

Group: Administration
Posts: 1,748
Joined: 27-April 02
From: In teh pastures
Member No.: 162

Sex: Male



the short tag, <?, is generally not used cause it brings up problems in certain environments.

but yar, there usualy is a shorter way to do anything
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post Jun 19 2004, 06:17 AM
Post #4


kiefpiet
Group Icon

Group: Regular Member
Posts: 1,881
Joined: 28-April 02
From: Benoni
Member No.: 163

Sex: Male



Also keep on checking http://www.php.net/downloads.php for the latest version of php and any fixes

and

http://dev.mysql.com/downloads/ for the latest version of mySQL and any fixes.


--------------------
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post Jun 19 2004, 06:58 AM
Post #5


I ain't never met someone who didn't like parfait
Group Icon

Group: Regular Member
Posts: 355
Joined: 16-April 03
From: Johannesburg
Member No.: 1,077




well when I decided to learn php I just read the manual.
I wouldnt suggest it if you have not learned another language before but it didnt take me long at all to learn php through it.

its also v. easy if you know another scripting lang like perl


--------------------
"I do not fear computers. I fear the lack of them."
Isaac Asimov (1920 - 1992)

Master Foo once said to a visiting programmer: "There is more Unix-nature in one line of shell script than there is in ten thousand lines of C."

-5+7=2 ... so if you dont have 5 apples and you get 7 more than you have TWO apples
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post Jul 21 2004, 02:15 AM
Post #6


CoOkIeMoNsTeR
Group Icon

Group: Regular Member
Posts: 558
Joined: 2-May 03
From: Durban
Member No.: 1,112




You have highlighted what is required to get started with the programming. But what is required for getting a server up & running? I'm assuming its a linux box with a webserver installed (Apache?) and PHP with MySql doing the database stuff. Now I'm a complete n00b when it comes to Linux but I'd like to start learning how to develop web apps etc with PHP. What do I need to get started?


--------------------
slouzbry stouvre for keykey...ooo woow!
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post Jul 21 2004, 02:58 AM
Post #7


wild bastard
Group Icon

Group: Administration
Posts: 5,168
Joined: 4-June 02
From: Johannesburg South Africa
Member No.: 1

Sex: Male



there are windows binaries too if you so wish.


You can get apache for windows , php for windows and mysql for windows.

so before trying to learn linux/unix php apache and mysql all at once, ask yourself what you wish to learn 1st ?

I'd suggest learning a bit of php now and tinker around with linux in your spare time if programming is what you wanna do.

Theres an exe that will install apache and the documentation is written in easy to understand english

start there then install php and mysql for windows.

enjoy


--------------------
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post Jul 21 2004, 04:00 AM
Post #8


~Pulsage~
Group Icon

Group: Regular Member
Posts: 551
Joined: 1-May 02
From: South Africa - Gauteng
Member No.: 179

Sex: Male



Just a recap of those URL links for downloads:

Apache HTTP Server

PHP

MySQL

Pulse punk.gif
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post Jul 21 2004, 04:17 AM
Post #9


wild bastard
Group Icon

Group: Administration
Posts: 5,168
Joined: 4-June 02
From: Johannesburg South Africa
Member No.: 1

Sex: Male



actually download php from the snapshot repository if you are planning on installing php 5

http://snaps.php.net/

the latest release has a bug fix for certain issues
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post Jul 21 2004, 07:14 AM
Post #10


CoOkIeMoNsTeR
Group Icon

Group: Regular Member
Posts: 558
Joined: 2-May 03
From: Durban
Member No.: 1,112




Thanks very much guys, in answer to your question Rush, I want to learn how to develop web applications - personal blog type systems and perhaps some marketable e-commerce front end etc. I'd like to learn how to setup a webserver etc on Linux just so I know how to do it
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post Jul 21 2004, 07:19 AM
Post #11


wild bastard
Group Icon

Group: Administration
Posts: 5,168
Joined: 4-June 02
From: Johannesburg South Africa
Member No.: 1

Sex: Male



setting up apache on linux is is as easy as "emerge apache" on gentoo linux then editing your conf files, its piss easy.
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post Jul 22 2004, 01:33 AM
Post #12


CoOkIeMoNsTeR
Group Icon

Group: Regular Member
Posts: 558
Joined: 2-May 03
From: Durban
Member No.: 1,112




Lol! You make it sound so simple smile.gif Thanks for the advice.
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post Jul 22 2004, 02:06 AM
Post #13


Not The Normal Kind Of Nutty
Group Icon

Group: Regular Member
Posts: 483
Joined: 18-July 02
From: PTA
Member No.: 463

Sex: Male



Thanx a million for this link guy's, i've been wanting to try out php for a while now but never knew where to start & to be honest just to lazy to start looking (& the fact that i don't have i-net at home doesn't help either) but know i can download all the stuff at work & go play at home biggrin.gif

Rush: You say it worh learning php in windows first & then learning linux as you go along??

Thanx again....

(Add link to favourites)


--------------------
I Live In My Own Little World. But It's OK. They Know Me Here.

<img src="http://www.net-forums.net/fred/sig/stat.php?pic=1&username=BadBoyTazz4Ever" alt="Project Fred Stats" border="0">
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post Jul 22 2004, 03:13 AM
Post #14


wild bastard
Group Icon

Group: Administration
Posts: 5,168
Joined: 4-June 02
From: Johannesburg South Africa
Member No.: 1

Sex: Male



well, you can learn php without knowing linux at all because its a cross platform language, you can install apache , mysql and php on windows.

So if linux is not your main interest , why bother?
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post Jul 22 2004, 03:43 AM
Post #15


Not The Normal Kind Of Nutty
Group Icon

Group: Regular Member
Posts: 483
Joined: 18-July 02
From: PTA
Member No.: 463

Sex: Male



QUOTE(Rush @ Jul 22 2004, 12:13 PM)
well, you can learn php without knowing linux at all because its a cross platform language, you can install apache , mysql and php on windows.

So if linux is not your main interest , why bother?

I really do want to get into Linux but i only have 1 PC available right now & it's got Windows on with all my stuff!!! Yes i know you can dual boot but i don't have enough free space for a Linux install nor do i have a open ide point for another HDD!!!

If all goes well i'll be getting a bigger PC soon witch i'll make my windows PC & then i'll start learning Linux on my current PC & once i know enough about linux i'll go over completely...

But it's great know that i can learn a new language on something i know & once i know Linux more move over with out to much hassels if any!!!

Thanx i'll get the Windows once for now, but i'll keep the links for the linux once till i know linux better...
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post May 16 2005, 02:29 AM
Post #16


Advanced Member
Group Icon

Group: Regular Member
Posts: 229
Joined: 23-November 02
From: Durban South Africa
Member No.: 742

Sex: Male



i use apache2triad very easy to get everything running
http://sourceforge.net/projects/apache2triad/

Server bundle of : Apache2 , MySQL , PostgreSQL , OpenSSL , Xmail , SlimFTPd Software developing triad of : PHP , Perl and Python + Apache2TriadCP , PHPmyadmin , PHPPgAdmin , AWStats , UebiMiau , PHPXMail , PHPSFTPd. All latest stables , all manuals


--------------------
Recursion /ree-ker'-zhon/: See Recursion.
User is offlineProfile CardPM
+Quote PostGo to the top of the page

Reply to this topicStart new topic

Collapse

> Similar Topics

Topic Title Replies Topic Starter Views Last Action
No entries to display


 



- Lo-Fi Version Time is now: 19th March 2010 - 11:57 PM
Privacy Policy
South Africa's Top Sites Kinetiq