Computer Science Canada

Basic php

Author:  Amailer [ Wed Jul 16, 2003 5:55 am ]
Post subject:  Basic php

Starting off with the basic.

How do you start a php code?
What is the main thing?

code:

<?
....
?>


The <? means you are starting a PHP script.
And the ?> means your ending the php script.

How do you display something?
like how do you display "Hello compsci"?

code:

<?
echo "Hello compsci";
?>


The echo is used to display stuff on a web browser.
It can also display HTML.

Most people use echo to display their information.
But you can also use:

code:

<?
print "Hello compsci";
?>


It does the same thing as ECHO.

Now let make a small script with what we have learned.

code:

<?

echo "<B>Hello</B>";
echo "<BR> Welcome to the compsci.ca world";

// Or

print "<BR>Have a great day!";
?>


See?
Simple,
If you try this script and run it on your browser, you will see HELLO in BOLD.
And on another line you will see, Welcome to compsci.ca world and on the next line. Have a great day.

The <BR></BR> and <B></B> are HTML.

How like some other languages, we have variables.
For PHP the way you give a variable a vlue is like this

code:

<?

$variable = "Hello";

?>


When starting a variable you MUST put a $ before your variable name.

The = tells php that the variable name you made, its value is what ever.

Making it simpler...taking the sample code form above.

the variable $variable, its value is HELLO.

Now trying this out.
Displayign the infromation the variable is very simple.
You can do it by just using the echo or print we just learned.
I.E:

code:

<?

$variable = "Learned anything yet?";

echo $variable;

?>


Now if you run this small script on your brower, you sould see Learned anything yet displayed there.

Its displaying the value of the given variable..thats $variable Smile

Now rememebr...when you do
code:

...
echo $variable;
...


you cannot add anything to that line...what i mean is this:

code:

...
echo $variable more words more words!;
...


that will not work...so what you can do is 2 things.

code:

...
echo $variable . "more words more words";

//OR

echo "$variable more words more words";
....


Both of the above work Smile

Now you cannot do this also.

code:

...
echo $variable1 $variable2;
...


Nope that won't work...what you have to do is this.

code:

...
echo $variable1 . $variable2;
...

that would work prefrectly Smile


(BTW, i forgot about this but the // means your commening something, you can also use # for commenting).

Now to put what ever we learned to make a little script.

code:

<?

$variable1 = "Welcome";
$variable2 = "<br>To the greatest";
$variable3 = "compsci.ca";

echo $variable1 . $variable2 . "Site for compsci" . $variable3;

//OR

echo "<br>$variable1 $variable2 Site for compsci  $variable3;

?>


Now as you see the above script has 3 variables.
variables 1 as 'welcome'
variable 2 as '<br>To the greatest'
and variable 3 as 'compsci.ca'

And then we echo the variables with some other wording comming in the middle to finish the sentrence 'site for compsci'

And the example of another way you could do it

code:

...
echo "<br>$variable1 $variable2 Site for compsci  $variable3;
...




Well now since we have learned all that we needed to,
we can just move on to if statements.

Well lets show you how if statements work.

If statements are really great to use, comes in handy every singe time Smile

In english this is how an if statement works

If there are 2 computers ...
i will buy this game. Else i will buy no game


And in other words.
if(there are2 computers) {
i will buy this game
} else {
i will buy no games
}

in PHP :

code:

...
if($computers == 2) {
echo "i brough BF 1942";
} else {
echo "i did not buy any games :(";
}
...


That should make it VERY simple Smile

When your using an if statement, and you want to see if something equles to something...you use tow =.

There are many things you can do with if statements
for example.

In english...


if these neards intellegents are not equal to these neards intelligents. KILL THEM ALL
else if these neards intellengents equal to those neards intellengets
Let them live


Ok so you don't know what you will do for the 'not equal to' part?
Well like math php makes english shorter...in php 'not equal to' is !=

So lets convert this into PHP Smile
code:

...
if($neards1 != $neards2) {
echo "DIE NEARDS DIEEEEEEE! AHAHA DIE";
} elseif($neards1 == $neards2) {
echo "Ahh rats...can't kill you now :(";
}
...


Now you will know everything over there....except one thing..where did that 'elseif' come from?

Well elseif is just comining and else and if togeater Smile

you can do this
code:

...
if(...) {
...
} elseif(...) {
....
} else {
...
}
...


Allows you to do more stuff after you have just done 1 if statement...
so we could do this.

code:

...
if($neards1 != $neards2) {
echo "DIE NEARDS DIEEEEEEE! AHAHA DIE";
} elseif($neards1 == $neards2) {
echo "Ahh rats...can't kill you now :(";
} else {
echo "...where all the neards go?";
}
...




Now im sure you all know HTML (DUH YOU HAVE TO!!!)

So lets see how we can use PHP in form posting...

As we all know in a form you have 2 methods.


  • POST
  • GET


Lets first work with the POST method.

Now we first creat a form in HTML:

HTML:
code:

<form method="POST" action="post.php>
  <p>
  Ya name dammit: <input type="text" name="name" size="20"><br>
  <input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>


Now what we have in this form is a text box and a submit button...
And the action is to use the method 'POST' to post it to the php file post.php.

Now what we need to do is create a file,
name it post.php

Now in it all we need to do is use ECHO Smile

I.E:
code:

<?

echo $name;

?>


Ok so...your think hard...where on earth did i get that variable $name?
Hmm so stop thinking...you got your form?

(hope you saved it in a HTML file..)

Run it on your brower....

WAIT!!! STOPP!!!
Forgot something...
First you need to keep the post.php file in the same place the html file that the form is keeped..
Once your done all that...Run your HTML file that contains the form.

Assuming that you named your HTML file 'posting.html'

And you run it on your brower...you should see the form...
type in a name (your name anything).

Once you click submit...you will be sumitting the VALUE typed in the text box 'NAME'.

And the information is been given to the file 'post.php'

And then our small php script works...it gets the value (the variable name is from the text box name). And then it echos it Smile

Simple?

Hope so....
Anyways...

lets USE what ever we learned and make a tiny SCRIPT...
Ok

code:

HTML

<form method="POST" action="check.php">
  <p>
  Compsci.ca is the best site on earth?<br>
  <input type="checkbox" name="compsci" value="YES" checked><br>
  <font size="1">CHECK IF YES, UNCHECK IF NO</font><br>
  <input type="submit" value="Submit" name="B1"></p>
</form>

SAVE IN 'COMPSCI.HTML'


And now for the CHECK.PHP
Keep in the same place as the COMPSCI.HTML

code:

<?
//PHP
//Check.php

if($compsci == "YES") {
echo "DUH MAN YOUR SO TRUE!";
} elseif($compsci != "YES") {
print "WHAT ON EARTH...GET A LIFE";
} else {
echo "make up your mind...";
}

//END OF CODE
?>


Ok SIMPLE STUFF EH?!
VERY HUH?!

All this does is checks to see if the person things copsci.ca is the best site on earth..if the person says yes, it will display the message "DUH MAN YOUR SO TRUE!"
else if its not yes it will display the message "WHAT ON EARTH...GET A LIFE"

or if nothing [ this is not possiable ]

it willl display the message "make up your mind..."

Now for the last part

code:

...
} else {
echo "make up your mind...";
}


Does not make sence having it does it?

Well insted of doing that...lets try this


code:

<?
//PHP
//Check.php - update

if($compsci == "YES") {
echo "DUH MAN YOUR SO TRUE!";
} elseif($compsci != "YES") {
print "WHAT ON EARTH...GET A LIFE";
} else(!isset($compsci)) {
echo "make up your mind...";
}

//END OF CODE
?>


!isset tell you that there is no such variable...and do some crap about it!

Author:  PaddyLong [ Wed Jul 16, 2003 12:02 pm ]
Post subject: 

hmm... one thing for the forms
you need to use $_POST['variablename'] or $HTTP_POST_VARS['variablename']
and
$_GET['variablename'] or $HTTP_GET_VARS['variablename']

(the HTTP_blah_VARS are from older versions of PHP, so I tend to use those to make sure the script will work on any server... best to use them to start with rather than going back to fix them all later Very Happy)


if the scripts are working without those then it means that register_globals is set to true in the PHP config file, which from what I have read is bad for security... and also it probably isn't set on most web hosts

as an alternative you could look at the import_request_variables

Author:  krishon [ Wed Jul 16, 2003 6:30 pm ]
Post subject: 

not bad tut...i haven't learned php and i get wut u mean

Author:  Amailer [ Wed Jul 16, 2003 6:59 pm ]
Post subject: 

PaddyLong wrote:
hmm... one thing for the forms
you need to use $_POST['variablename'] or $HTTP_POST_VARS['variablename']
and
$_GET['variablename'] or $HTTP_GET_VARS['variablename']

(the HTTP_blah_VARS are from older versions of PHP, so I tend to use those to make sure the script will work on any server... best to use them to start with rather than going back to fix them all later Very Happy)


if the scripts are working without those then it means that register_globals is set to true in the PHP config file, which from what I have read is bad for security... and also it probably isn't set on most web hosts

as an alternative you could look at the import_request_variables



yes thx,
But the other way works also Confused

Author:  PaddyLong [ Thu Jul 17, 2003 2:13 am ]
Post subject: 

the other way will only work if you have register_globals on in the php config file on the server.... it defaults to off since 4.2.0 and writing your php with it on is considered sloppy


: