php for dummies issue 3 p1
Posted 07-09-2009 at 01:36 PM by kikuta akio
Hey everyone, I will be running a series of articles based on educating all of you in the wanders of PHP, feel free to follow along it is very easy to learn. If you happen to miss an issue then please go and view my blog on the forums where I will have a whole backlog of articles.
This article will be mainly talking about the data types available to you as a programmer, as well as their uses. This issue will focus on almost setting up your own “cheat sheet” with the different data types and their functions, as well as how to use them in the programming of your first program. The next exciting issue will be focused on enhancing your program, and future issues will incorporate SQL tables into your code.
This article is mostly aimed at those who have no programming knowledge, and so it only covers the basics. More in-depth information can be obtained from a variety of sources. For iPod touch/iPhone users check out the great PHP cheat sheet app.
The Basics
Variables: are used for storing a value like text strings, numbers, and arrays. Once a variable has been established it can be used anywhere throughout your script. All variables in PHP will begin with a $.
Rules for variables:
-A variable name must start with an underscore “_” or a letter
-A variable name can only contain alphanumeric characters, and underscores (a-z, A-Z, 0-9, _)
-A variable name should not contain spaces, if multiple words are used they can be separated with an underscore or capitalization e.g $UserName or $user_name
Examples:
Strings: are used to store and manipulate a piece of text. The most common string you will use is echo. There are hundreds of string functions, such as bin2hex(), this string function will turn whatever binary code that is in the brackets into a hexadecimal code
Operators: these are the functions that operate on values. I have included a table below of the operators.
Arithmetic Operators
[table]
[tr]
[th]Operator[/th]
[th]Description[/th]
[th]Example[/th]
[/tr]
[tr]
[td]+[/td]
[td]Addition[/td]
[td]x+2[/td]
[/tr]
[tr]
[td]-[/td]
[td]Subtraction[/td]
[td]7-x[/td]
[/tr]
[tr]
[td]*[/td]
[td]Multiplication[/td]
[td]x*4[/td]
[/tr]
[tr]
[td]/[/td]
[td]Division[/td]
[td]12/6[/td]
[/tr]
[tr]
[td]%[/td]
[td]Modulus[/td]
[td]10%8[/td]
[/tr]
[tr]
[td]++[/td]
[td]Increment[/td]
[td]x++[/td]
[/tr]
[tr]
[td]--[/td]
[td]Decrement[/td]
[td]x--[/td]
[/tr]
[/table]
Assignment Operators
[table]
[tr]
[th]Operator[/th]
[th]Description[/th]
[th]Example[/th]
[/tr]
[tr]
[td]=[/td]
[td]x=y[/td]
[td]x=y[/td]
[/tr]
[tr]
[td]+=[/td]
[td]x+=y[/td]
[td]x=x+y[/td]
[/tr]
[tr]
[td]-=[/td]
[td]x-=y[/td]
[td]x=x-y[/td]
[/tr]
[tr]
[td]*=[/td]
[td]x*=y[/td]
[td]x=x*y[/td]
[/tr]
[tr]
[td]/=[/td]
[td]x/=y[/td]
[td]x=x/y[/td]
[/tr]
[tr]
[td].=[/td]
[td]x.=y[/td]
[td]x=x.y[/td]
[/tr]
[tr]
[td]%=[/td]
[td]x%=y[/td]
[td]x=x%y[/td]
[/tr]
[/table]
Comparison Operators
[table]
[tr]
[th]Operator[/th]
[th]Description[/th]
[th]Example[/th]
[/tr]
[tr]
[td]==[/td]
[td]is equal to[/td]
[td]5==8 returns false[/td]
[/tr]
[tr]
[td]!=[/td]
[td]is not equal[/td]
[td]5!=8 returns true[/td]
[/tr]
[tr]
[td]>[/td]
[td]is greater than[/td]
[td]5>8 returns false[/td]
[/tr]
[tr]
[td]<[/td]
[td]is less than[/td]
[td]5<8 returns true[/td]
[/tr]
[tr]
[td]>=[/td]
[td]is greater than or equal to[/td]
[td]5>=8 returns false[/td]
[/tr]
[tr]
[td]<=[/td]
[td]is less than or equal to[/td]
[td]5<=8 returns true[/td]
[/tr]
[/table]
Logical Operators
[table]
[tr]
[th]Operator[/th]
[th]Description[/th]
[th]Example[/th]
[/tr]
[tr]
[td]&&[/td]
[td]and[/td]
[td](6 [<10 && 3 > 1) returns true[/td]
[/tr]
[tr]
[td]||[/td]
[td]or[/td]
[td](6==5 || y==3) returns false[/td]
[/tr]
[tr]
[td]![/td]
[td]not[/td]
[td]!(6==3) returns true[/td]
[/tr]
[/table]
This is part one, but already I have shown you enough to create your first program!
Ok open up notepad, or notepad++(I recommend you download this to check your code as you go along)
and then enter the data below into a page that you save as calculator.php (this page is actually in HTML code, and the assumption was made that you had a knowledge of that)
This is merely your calculator page, time for the functions of this page, open another page and call it calculate.php, this is a simple addition php page, comments are within
This article will be mainly talking about the data types available to you as a programmer, as well as their uses. This issue will focus on almost setting up your own “cheat sheet” with the different data types and their functions, as well as how to use them in the programming of your first program. The next exciting issue will be focused on enhancing your program, and future issues will incorporate SQL tables into your code.
This article is mostly aimed at those who have no programming knowledge, and so it only covers the basics. More in-depth information can be obtained from a variety of sources. For iPod touch/iPhone users check out the great PHP cheat sheet app.
The Basics
Variables: are used for storing a value like text strings, numbers, and arrays. Once a variable has been established it can be used anywhere throughout your script. All variables in PHP will begin with a $.
Rules for variables:
-A variable name must start with an underscore “_” or a letter
-A variable name can only contain alphanumeric characters, and underscores (a-z, A-Z, 0-9, _)
-A variable name should not contain spaces, if multiple words are used they can be separated with an underscore or capitalization e.g $UserName or $user_name
Examples:
PHP Code:
<?php
$text = ("Hello World");
$first_number = 3;
$SecondNumber = 7;
?>
PHP Code:
<?php
echo ($text)
?>
Arithmetic Operators
[table]
[tr]
[th]Operator[/th]
[th]Description[/th]
[th]Example[/th]
[/tr]
[tr]
[td]+[/td]
[td]Addition[/td]
[td]x+2[/td]
[/tr]
[tr]
[td]-[/td]
[td]Subtraction[/td]
[td]7-x[/td]
[/tr]
[tr]
[td]*[/td]
[td]Multiplication[/td]
[td]x*4[/td]
[/tr]
[tr]
[td]/[/td]
[td]Division[/td]
[td]12/6[/td]
[/tr]
[tr]
[td]%[/td]
[td]Modulus[/td]
[td]10%8[/td]
[/tr]
[tr]
[td]++[/td]
[td]Increment[/td]
[td]x++[/td]
[/tr]
[tr]
[td]--[/td]
[td]Decrement[/td]
[td]x--[/td]
[/tr]
[/table]
Assignment Operators
[table]
[tr]
[th]Operator[/th]
[th]Description[/th]
[th]Example[/th]
[/tr]
[tr]
[td]=[/td]
[td]x=y[/td]
[td]x=y[/td]
[/tr]
[tr]
[td]+=[/td]
[td]x+=y[/td]
[td]x=x+y[/td]
[/tr]
[tr]
[td]-=[/td]
[td]x-=y[/td]
[td]x=x-y[/td]
[/tr]
[tr]
[td]*=[/td]
[td]x*=y[/td]
[td]x=x*y[/td]
[/tr]
[tr]
[td]/=[/td]
[td]x/=y[/td]
[td]x=x/y[/td]
[/tr]
[tr]
[td].=[/td]
[td]x.=y[/td]
[td]x=x.y[/td]
[/tr]
[tr]
[td]%=[/td]
[td]x%=y[/td]
[td]x=x%y[/td]
[/tr]
[/table]
Comparison Operators
[table]
[tr]
[th]Operator[/th]
[th]Description[/th]
[th]Example[/th]
[/tr]
[tr]
[td]==[/td]
[td]is equal to[/td]
[td]5==8 returns false[/td]
[/tr]
[tr]
[td]!=[/td]
[td]is not equal[/td]
[td]5!=8 returns true[/td]
[/tr]
[tr]
[td]>[/td]
[td]is greater than[/td]
[td]5>8 returns false[/td]
[/tr]
[tr]
[td]<[/td]
[td]is less than[/td]
[td]5<8 returns true[/td]
[/tr]
[tr]
[td]>=[/td]
[td]is greater than or equal to[/td]
[td]5>=8 returns false[/td]
[/tr]
[tr]
[td]<=[/td]
[td]is less than or equal to[/td]
[td]5<=8 returns true[/td]
[/tr]
[/table]
Logical Operators
[table]
[tr]
[th]Operator[/th]
[th]Description[/th]
[th]Example[/th]
[/tr]
[tr]
[td]&&[/td]
[td]and[/td]
[td](6 [<10 && 3 > 1) returns true[/td]
[/tr]
[tr]
[td]||[/td]
[td]or[/td]
[td](6==5 || y==3) returns false[/td]
[/tr]
[tr]
[td]![/td]
[td]not[/td]
[td]!(6==3) returns true[/td]
[/tr]
[/table]
This is part one, but already I have shown you enough to create your first program!
Ok open up notepad, or notepad++(I recommend you download this to check your code as you go along)
and then enter the data below into a page that you save as calculator.php (this page is actually in HTML code, and the assumption was made that you had a knowledge of that)
PHP Code:
<form name="form" method="post" action="calculate.php">
<table width="263" border="0">
<tr>
<td width="117"><p>Enter number 1</p></td>
<td width="130"><input type="text" name="number1"></td>
</tr>
<tr>
<td height="26"><p>Enter number 2</p></td>
<td><input type="text" name="number2"></td>
</tr>
</table>
<input name='run' type='submit' value='run'>
</form>
PHP Code:
<?php //this begins php scripts
$num1 = $_POST['number1']; //this collects the first number from the other page, the post part means that it listens for what the first page sent to it, and the part that was defined as "number1" is defined as $num1
$num2 = $_POST['number2'];
$total = $num1 + $num2 ; // this is a basic, it defines $total as the simple addition equation"
echo "the total is $total" // this will display the total on your page
;
// this ends php scripts ?>
Total Comments 0
Comments
Total Trackbacks 0













