Welcome Guest [Log In] [Register]
Welcome to New Nsider. We hope you enjoy your visit.


You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
  • Pages:
  • 1
  • 3
PHP - Lesson 2; Basics of PHP
Topic Started: Jul 15 2006, 10:20 PM (580 Views)
Cube
Member Avatar
White Pikmin
Note: If you have not gone over lesson 1, is it recommended you do before reading this lesson.

Well, here is another lesson, this time it's about variables, data-types, and operators [and expressions]. To start off, let's go over what a variable is...

Intro to Variables:

Variables can change value, through many ways...including user input, hard coded, database queries, or data changed (there are more, but they aren't important to list).

A variable in php must start with the dollar sign ($). This would be a typical php variable...

$new_var;

...Simple enough, right? Well, first you need to remember these rules for variables in php.

-Variables cannot include spaces, they can include letters, numbers, and underscore characters (_), but not spaces.

Note, you'll find it best to make your variables meaningful. If you're not going to make meaningful variables, it would be wise to add comments next to them at least.

The semicolon is needed, it ends a PHP statement, without it, you'll get errors.

Now, you'll want to assign values to variables, so you would do the following...

$variablename = 90;

Now, if you echoed $variablename (echo $variablename;) then it would be the same as if you echoed 90 (echo 90;). Notice in those examples I did not put quotes around the echo statement, that is because of the datatype, they are numbers, not strings.

Superglobals:

PHP also have some built in variables that will be vary useful in many scripts. Here is a list of them...

$_GET
$_POST
$_COOKIE
$_FILES
$_ENV
$_REQUEST
$_SESSION

We'll go over their uses later, for now, let's move onto data types...

Data Types:

The standard data types...

Boolean - true or false
Integer - Whole number
Float or Double - decimal number
String - Characters/letters
Object - an instance of a class, I myself have not used this.
Array - Multiple items, keys, or values.
Resource - Things such as a database.
NULL - Nothing, a variable such as $var_name; is null, as nothing is assigned.

You may use settype() to change the data type of a variable. This can be useful when working with user input. An example of settype() would be...

Code:
 

<?php
$var = "1sjke";
settype($var, 'integer');
?>


As a string, you would notice 1sjke is displayed, but when converted to an integer, PHP only displays the number, being 1.

You may use gettype() to find what the current data type is if you wish. For example...

Code:
 

<?php
$var = false;
gettype($var);
?>


...would output boolean to the browser.

Now, you can as well change data type by casting, which makes a copy of the variable, and stores it into another variable, leaving the original as it was. An example of casting...

Code:
 

<?php
$var = "80";
$new_var = (integer) $var;
?>


...you'll notice that (integer) is what actualy changes the variable, while $var is the variable to change. Now, we'll move onto operators and expressions.

Operators and Expressions:

First, there are a few types of operators...they are as follows.

Assignement Operator is...
$name = "name"; // The equal sign is used to assign in php.

Arithmetic Operators are...
+ //Addition
- //Subtraction
/ //Division
* //Multiplication
% //Modulus

Concatenation Operator is...
"hello"." world" // The period smashed the two words together. Note the extra space in " world".

Combined Assignment Operators are...
+=
-=
/=
*=
%=
.=

I've never used combined assignement operators, but they make it shorter code if you do.

Incrementing Variables Automatically (Integer Only)...

$y = $y + 1; //First way to do this...increments by one
$y += 1; //Second way to do this...increments by one
$y++; //Third way to do this, and easiest...increments by one

The same goes for decrementing. Now, you can also place the operators behind the variable to ensure that the variable is incremented before any sort of test with it is carried out (++$y;), it can be useful at times.

Comparison Operators are...

These will be extremely useful for things such as loop, and many other uses. Let's say you want to make sure $y is smaller than 8, you can do that by do this...

$y < 8

Depending on the value $y is assigned to, the result may vary. Here are all the comparison operators...

== //Equivalence
!= //Non-equivalence
=== //Identical
> //Greater than
>= //Greater than or equal to.
< //Less than
<= //Less than or equal to.

Now, let's go over Logical Operators...

Logical Operators are...

|| //OR
xor //Xor
&& //And
! //Not

For example, here is an example in php...

Code:
 

<?php
if(1 || 2 == 3) {
//Execute this.
}
?>


I left out some boolean operators that do the same as the above, but take longer to process. This concludes lesson 2...

Assignments:

-Convert a string variable to an integer, use gettype() and find the type. Echo the variable after converting.
-Make a short logical comparision using the logical operators. Does not have to be PHP.
-Increment a variable. Echo that variable after incremeting (both before executed and after executed incrementation).

Members who have completed this lesson:

Zero the Warrior, A
Posted Image
Offline Profile Quote Post Goto Top
 
Cube
Member Avatar
White Pikmin
Well, this lesson is up...a rather long one as well, post any questions you have.
Posted Image
Offline Profile Quote Post Goto Top
 
Phantom123
Member Avatar
Covenant Elite
Cube, I'll try to do a Lesson a day...This is long, but I think a day oughta be enough time for anyone if they work on it.
<img src=http://pic.piczo.com/img/i15885113_21172_2.gif>

Best. Objection. Ever.
Offline Profile Quote Post Goto Top
 
Cube
Member Avatar
White Pikmin
Phantom123
Jul 16 2006, 10:04 AM
Cube, I'll try to do a Lesson a day...This is long, but I think a day oughta be enough time for anyone if they work on it.

Heh. A a lesson/chapter in my book is longer than this, usualy about 20 or so pages. >_>

...and I read through it in around 5 to 6 days.

Oh, and you still have to finish lesson 1 assignments. >_>
<_<
Posted Image
Offline Profile Quote Post Goto Top
 
Cube
Member Avatar
White Pikmin
Apparently people must be extremely confused by this lesson...

Please, post what you're confused on, and I'll do what I can to help you understand it.
Posted Image
Offline Profile Quote Post Goto Top
 
Phantom123
Member Avatar
Covenant Elite
I've just been busy, actually o.o' I'll do it today or in the next couple of days
<img src=http://pic.piczo.com/img/i15885113_21172_2.gif>

Best. Objection. Ever.
Offline Profile Quote Post Goto Top
 
Link
Member Avatar
Leader of the Link_109's! >.>
I don't get anything from here and the simplified one in lesson one... o.o;
And I give it all away
Just to have somewhere
To go to
Give it all away
To have someone
To come home to
Offline Profile Quote Post Goto Top
 
Cube
Member Avatar
White Pikmin
Link
Jul 18 2006, 06:05 PM
I don't get anything from here and the simplified one in lesson one... o.o;

Well, in that case...

Do you know HTML? Or any web design languages? >->
Posted Image
Offline Profile Quote Post Goto Top
 
Link
Member Avatar
Leader of the Link_109's! >.>
Nope. Should I?
And I give it all away
Just to have somewhere
To go to
Give it all away
To have someone
To come home to
Offline Profile Quote Post Goto Top
 
Dragonking123
Things
._. Reminds me somewhat of C++...sorta...kinda.......just a smidge. ._.
Posted Image
Offline Profile Quote Post Goto Top
 
Go to Next Page
« Previous Topic · Archive · Next Topic »
Add Reply
  • Pages:
  • 1
  • 3

Theme created by tiptopolive. Find more great themes and skins at the ZB Theme Zone.