Jump to content


Photo
- - - - -

[PHP] - [PHP Basics] - [dEcade]


  • Please log in to reply
10 replies to this topic

#1 dEcade

dEcade

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 1,850 posts
  • Gender:Male
  • Location:Saskatoon, Saskatchewan
  • Interests:Guitar, Programming, Storm Chasing, Games (Designing and playing), Hockey, Photography

Posted 27 March 2005 - 08:01 PM

Code 1
here is a very simple
<?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!
<?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.
<?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.
<?php ?>
first start off with your tages. Put the code that I show you below into thoughs tages.
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.
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 ;
}
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!
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.
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!
<?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!
<?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
<?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:
elseif($lang == hello){
echo 'This is just going to be hello';
}
after the:
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

#2 Jamie Huskisson

Jamie Huskisson

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 3,648 posts
  • Gender:Male
  • Location:Nottingham, UK

Posted 28 March 2005 - 09:32 AM

wow nice, thanks for posting this dEcade :P

#3 Spooky

Spooky

    Rooster

  • Members
  • PipPip
  • 463 posts
  • Gender:Male
  • Location:Littleton, CO
  • Interests:Web Design, Programming, Client Management, Web Hosting, Tutorial Writing

Posted 28 March 2005 - 09:59 AM

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. =)

#4 dEcade

dEcade

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 1,850 posts
  • Gender:Male
  • Location:Saskatoon, Saskatchewan
  • Interests:Guitar, Programming, Storm Chasing, Games (Designing and playing), Hockey, Photography

Posted 28 March 2005 - 10:08 AM

There I change all of the starting tags to <?php now hope fully people won't get cunfused :P

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

#5 adam123

adam123

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 2,306 posts
  • Location:London, UK
  • Interests:Programming and stuff.

Posted 28 March 2005 - 01:35 PM

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.

#6 Apache

Apache

    P2L Jedi

  • Twodded Staff
  • PipPipPip
  • 778 posts
  • Location:London, UK

Posted 28 March 2005 - 04:51 PM

dont you have to put the hello in quotes, so it should be

if($lang == 'hello') {

???

#7 dEcade

dEcade

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 1,850 posts
  • Gender:Male
  • Location:Saskatoon, Saskatchewan
  • Interests:Guitar, Programming, Storm Chasing, Games (Designing and playing), Hockey, Photography

Posted 28 March 2005 - 06:18 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

#8 Griffin

Griffin

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 1,207 posts
  • Gender:Male
  • Location:California
  • Interests:Things I find interesting.

Posted 03 April 2005 - 04:29 PM

This is so beyond me lol....nice tutorial, you guys seem to be really good at this

#9 techo

techo

    Young Padawan

  • Members
  • Pip
  • 3 posts

Posted 05 April 2005 - 03:48 PM

Isnt this off w3schools?

#10 dEcade

dEcade

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 1,850 posts
  • Gender:Male
  • Location:Saskatoon, Saskatchewan
  • Interests:Guitar, Programming, Storm Chasing, Games (Designing and playing), Hockey, Photography

Posted 02 May 2005 - 09:12 AM

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:

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

them it would just show p2l rules

dEcade

#11 Hoot

Hoot

    * Hooty Tooty Fruit *

  • Members
  • PipPipPipPip
  • 1,346 posts
  • Gender:Male
  • Location:Canada
  • Interests:Making Websites, Working in 3D, Playing the Drums

Posted 02 May 2005 - 08:50 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users