Peep in my mind

Tags: , ,

Numbers

Posted on 11 June 2009 by admin

php101
Doing math in php

$variable = value * value
$variable = value + value
$variable = value – value
$variable = value / value

Formatting Numbers

Rounding numbers – rounds values to specified decimal places. It can be number or variables. There is an option that allow users to round too the number of decimal places.

Example:
round (6.20); = 6
round (6.369, 2); = 6.37
$number = 694.2783;
round ($number); = 694

Number format – works like round but adds commas in the to numbers.
number_format (6.2243, 2); = 6.22
number_format (69, 2); = 69.00
number_format (123456789); = 123,456,789

Precedence - the order in which calculations are made.

8 – 4 / 2 = 6 and not 2 because division comes first.

To avoid this use ()
(8 – 4) /2 ) = 2
8 – (4 / 2 ) = 6

Incrementing and decrementing a number add or subtracts 1 value
$var++; or $var–; this will give the value +1 or -1 effect.

Creating random numbers

rand()

$n = rand();- this generate random number
$n = rand(0,100); – this generate random number 0 to 100

Other types are getrandmax() – get height random number
mt_rand() – it suppose to work better then rand and great for cryptography.

These notes are from PHP for the World Wide Web, Third Edition (Visual QuickStart Guide) written by Larry Ullman. In order to make sense of my notes you should pruchase the PHP book its very well written and easy to follow.

Comments (0)

Tags: , , ,

Creating Forms with HTML and PHP

Posted on 10 June 2009 by admin

php101

Forms

Code to generate forms goes between <form> data </form> tags.  In the opening form tag also contains an action attribute.  This indicates which page the form data should submit.
<form action=”submit.php”>

There are two methods of transmitting data from the form to the handling script. They are GET and POST.  The GET method sends all the gathered information along as part of the url. The Post method sends the data invisibly.

Example of the GET method.

http://www.blah.com/page.php?var=value&blah…

Example of the POST method.

http://www.blah.com/page.php

Notes to keep in mind about GET method.

  • GET method is limited amount of data that can be transferred.
  • Data can be view publicly which may cause security risk.
  • A page created by GET method can be bookmarked.
  • GET pages can be reloaded POST pages will have a confirmation box

To used the form data submitted users need to use predefined variables.  They are $_GET and $_POST they are used based on which type of method is picked when generating the form.  If the method was post then use $_POST if the method was GET then use $_GET.  Also the variable must be typed with all caps cause it is case sensitive.

Sample code form.html

<body>
<form action=”display_form.php” method=”post”>
<p>Comments: <textarea name=”comments” rows=”3″ cols=”30″></textarea></p>
<input type=”submit” name=”submit” value=”Send Comment” />
</form>
</body>

Sample code display_form.php

<body>
<?php
$comments = $_POST['comments'];
print “<p>Thank you for the comment <br />$comments</p>”;
?>
</body>

These notes are from PHP for the World Wide Web, Third Edition (Visual QuickStart Guide) written by Larry Ullman. In order to make sense of my notes you should pruchase the PHP book its very well written and easy to follow.

Comments (0)

Tags: , ,

PHP Variable Notes

Posted on 09 June 2009 by admin

php101

Variable – is like a magic box that stores information once the information is stored in the box it can be changed, or taken out to be displayed.

All variable names must be have a dollar sign “$” at the start of the variable.

After the dollar sign “$” the variable name must start with  a letter “A-Z” or “a-z” or a underscore “_” it cannot start with a number

After the “$” the rest of the name can be any combination of letters, numbers, and underscores.

Spaces can’t be used in variable names please use underscores “_” to represent spaces

Variable names are case sensitive so “$_cat” and “$_Cat” is different. (Try not to use variable with same names to reduce confusion).

Tips on creating variables

  • Always use lowercase on variable names
  • Always make your variable name descriptive
  • Always comment the purpose of the variables
  • Always try to defined all variables before use even though PHP doesn’t require it.

Variable Types

Numbers

There are two types of numbers in PHP Integers and floating-point.  Integers are whole numbers that can be both positive or negatives but cannot have fractions of decimals.  Floating-point numbers are use to if numbers have decimal places.  (The only way to use fraction in PHP is to covert to decimals).

Integer = 1 or -1

Floating-point = 1.02 or -1.02

Strings

A string is any number of characters enclosed in single or double quotation marks ‘string’ or “string”. Strings can be numbers letters symbols and spaces.  Stings can also contain variables. Strings can only contain 1 value.

To use quotation marks in a string use the backslash \.
Example: “Yo, “ho ho!”" would have to be written “Yo, \”hoho!\”" in order for it to display properly.

Different quotation marks can be used to fix the problem.
Example: “Yo, ‘ho ho!’” or ‘Yo, “ho ho!”‘.

Arrays

Arrays can contain a list of values so users can put multiple strings and numbers in to one array.

Array uses keys to create and retrieve that values stored. Php has two different types of arrays. An “Indexed array” uses numbers for its keys. An “Associative array” uses strings for its keys.  An array containing other arrays is called a “multidimensional array”.

Assigning Values to Variables

To assign a value to a variable the user must use the = sign.  The equal sign is called the “assignment operator” because it assigns a value to the variable.

Variable assignment operator value
$string = “Yo Ho!”;

Differences between Quotation marks ‘/”

Single quotation marks ” are treated literally
Double Quotation marks take the value of the variable.

These notes are from PHP for the World Wide Web, Third Edition (Visual QuickStart Guide) written by Larry Ullman. In order to make sense of my notes you should pruchase that excellent PHP book its very well written and easy to follow.

Comments (0)

Tags: , , , , , ,

PHP 101

Posted on 17 April 2009 by admin

php101

I started to pick up PHP and started reading this book PHP for the World Wide Web, Third Edition (Visual QuickStart Guide) written by Larry Ullman. Like any good student I like to make notes when I read then type it out mainly just so I can review what I just learned. (yeah it sound geeky but oh well only way i can learn) and since coding has never been my best subject I really do need to type it out over and over again. Anyways I figured I might as well keep my notes on my blog and share with you all. So if your reading my blog and trying to use my notes you should pick up the book too and follow along!

Definition of PHP

originally stood for Personal Home Page but it later changed and it now currently stands for Hypertext Preprocessor

  • php is an HTML embedded scripting language
  • php server side language
  • php allows users to create dyanmic pages
  • can run only on Php enabled web servers.

Basic PHP Syntax

  • PHP scripts should be save as a .php file
  • place php code within <? php code ?> tags
  • open “<?php”
  • closed “?>
  • php statements must end with “;” or error will occur.

Function

  • Functions in php are followed by parentheses () in which arguments are passed to the function.
  • php is case-insensitive when calling functions (variables are case sensitive in php).

Function: phpinfo()

  • this function list the spec of the php install on the sever
  • this is also a great way to test if php was installed properly on the server

Example :

<?php

// Show all information, defaults to INFO_ALL

phpinfo();

// Show just the module information.

// phpinfo(8) yields identical results.

phpinfo(INFO_MODULES);

?>

Function: print()

  • use to display text or Output a string
  • strings must be surrounded with ” ” numbers don’t.

Example:

<?php print (“Hello World”) ?>

Function: echo()

  • use to Output one or more strings

Example:

<?php echo (“Hello World”) ?>

Function: printf()

  • use to Output one or more strings

Example:

<?php printf (“Hello World”) ?>

To find more functions go to php.net./manual

Comments (2)

Advertise Here
Advertise Here

RELATED SITES