Help - Search - Members - Calendar
Full Version: [PHP] - [PHP Basics] - [dEcade]
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
dEcade
Code 1
here is a very simple
CODE
<?php
echo "Hello world!";
?>

the echo is what it will show in your browser!

code 2:
now we will do something a little trickier but not much!
CODE
<?php
if( $lang == 1 )
{
    echo "Hello World!";
}
?>

This code tells php that if the variables (The $ sign means it will be a variable) are $lang == 1 to show Hello World! so in the browser you would type index.php?lang=1

code 3:
now this is a very useful in php if you dont always want to keep puting new pages on the web you can make a whole bunch of pages in just one file!
Ok now we start writing the script, I will wright the full thing out then do it in parts and tell you a little about them.
CODE
<?php
if(!$lang){
echo "Hello World";
}
if($lang == hello){
echo "hello";
}
?>

There is a very simple one because sometimes there are many of thoughts thing over and over.
CODE
<?php ?>

first start off with your tages. Put the code that I show you below into thoughs tages.
CODE
if(!$lang){

this first part of the code is telling php that if its the variable lang then that will be the main part such as just index.php because it has that exclamation mark before the set variables.
CODE
echo "Hello World";

This is just telling what you want it to say when you are on that page, you can use eather " " or ' ' but when Im writing something like just text I use " " because if you are using ' ' and you need to write something like Can't (if you spell it right) then where the ' in cant is that would close that part and the wrest wouldn't be shown. If your writing code such as html in the php tags I use ' ' because in a lot of html there will be " so if you use ' ' then " wont close the script you are wrighting. Remember to always close with ;
CODE
}

after your are done the echo then hit ender which will bright you down a level and put } this shows that that part is done!

now for the ? mark part when you are in the browser!
CODE
if($lang == hello){

ok the if means if the statement in the brackets is true cary out the next part of the script. As you may notice there is no exclamation mark that is because we do not want this to be on the same page as the main one. Also you need to put the first part, lang (or what ever you set it as) the same every where on that page because if you don't then the text will show up on all the same pages. after the dobble equil sign the hello part is the part that will be after the equil sign in the browser so if you are having a whole bunch of pages in one you need to change it otherwise there will be a problem.
CODE
echo "hello";
}

the rest of this is the same as the first part.

ok now save it as index.php and put it on the web, then when you go to index.php it will say hello world, and when you put index.php?lang=hello it will say hello

code 4:
ok another good script and very simple is if you want to have a header and footer in different pages like phpbb does you can do this!
CODE
<?php include ("header.tpl"); ?>

<div align="center">
<?php
echo "main text";
?>

<?php include ("footer.tpl"); ?>

ok first things first you DONT need them to be .tpl they can be any type of web file or even a website.

code 5:
If you want to show someones IP adress you can use this script!
CODE
<?php
echo 'Your IP adress is: ';
echo $_SERVER["REMOTE_ADDR"];
?>




Part 2 - A little harder

Code 1
Here is another useful script so you do not have to make more pages. This script willl start off like the other one you would put ?lang=hello but with this script it will make it so you can make ?lang=hello&id=1
CODE
<?php
if(!$lang){
echo 'this is with nothing typed in';
}
if($lang == hello && $id == 1)
{
echo 'This is hello and id 1';
}
elseif($lang == hello){
echo 'This is just going to be hello';
}
?>

You notice how I put the:
CODE
elseif($lang == hello){
echo 'This is just going to be hello';
}

after the:
CODE
if($lang == hello && $id == 1)
{
echo 'This is hello and id 1';
}

This is because if I put it first, the second one would read off of the $lang variable then it would say something like "This is just going to be helloThis is hello and id 1" but if we put it second it does not think of the $lang as the variable to show that it reads off of the id=1 this is a help full script if you want th keep everything in the same category such, as say you were making a tutorial thing and you have php and html you could just have it ?lang=php&id=1 and ?lang=html&id=1 instead of getting them all mixed together.

dEcade
Jamie Huskisson
wow nice, thanks for posting this dEcade smile.gif
Spooky
Nice, few things... the <?php and <? set of tags were both used, you might want to stick it to one or the other so the new users to PHP understand a little better. Also, in the 10th code down from the top, you use the includes function, it should be include no s. =)
dEcade
There I change all of the starting tags to <?php now hope fully people won't get cunfused victory.gif

I actually sent these tutorials in but they didn't go on. meh what can you do there here now for all to see lol.

dEcade
adam123
Nice tutorial, only thing is, you should change the variable '$lang' to '$_GET['lang']' or '$HTTP_GET_VARS['lang']' to accomodate for people who have register_globals off.
Apache
dont you have to put the hello in quotes, so it should be

if($lang == 'hello') {

???
dEcade
QUOTE(Apache @ Mar 28 2005, 09:51 PM)
dont you have to put the hello in quotes, so it should be

if($lang == 'hello') {

???

no, it doesn't have to be. you can if you want I just do it that way.

dEcade
Griffin
This is so beyond me lol....nice tutorial, you guys seem to be really good at this
techo
Isnt this off w3schools?
dEcade
I don't think so, my bro posted what he new on my forums so I put them here.it is the basics that everyone should know, that why I posted it lol.

I am sure there are a lot more3 basic than this like this:

CODE
<?php
$hi = 'P2L rules';
echo $hi;
?>


them it would just show p2l rules

dEcade
Hoot
yes I did lol I never got this from any where just from my knowledge of PHP and the things that dEcade told you about the quotations, he is right it doesn't need them in that script but it wont hurt you if you want to put them in.

smardy
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.