Monday, February 7, 2011

Monday 2/7/2011


<?php
//Here we create and then call a simple function that outputs something.
function helloworld() {
echo "Hello World!
";
}
// We call it as easy as this:
helloworld();

//Creating and calling a function that accepts arguments is just as easy.
function saysomething($something){
echo $something . "
";
}
Saysomething("Hello World!"); //This would output "hello world!"

//and of course we can have our function return something as well.
function addvalues ($firstvalue, $secondvalue) {
return $firstvalue + $secondvalue;
}
$newvalue = addvalues (1,3);
echo $newvalue; //would echo "4".
?>


Here is another tutorial from PHP Recipes



<?php
//sample1_7.php

//Here is a variable. it is pretty easy to see it is a string.
$unknownvar = "Hello World!";
echo gettype ($unknownvar) . "
"; //will output string.
// the gettype is quite slow; the better way to do this is:
if (is_string ($unknownvar)){
//then do something with the variable.
echo "Is a string
";
}

?>


Here is another tutorial on casting.


<?php
//Let's say we start with a double value.
$mynumber = "1.03";
//and let's say we want an integer.
//we could do this:
$mynumber = settype ($mynumber, "integer");
echo $mynumber . "
"; //would output 1.
//but it is much better and looks far cleaner like this:
echo (int) $mynumber;

?>

Here is another tutorial.


<?php
// We are looking to receive a value from a "post" form before we search.
if (isset ($_POST['searchterm'])){
//then we would perform our search algorithm here.
}
else {
//or else, we generate an error.
echo "You must submit a search item. Please click the back button.";
}

?>


Here is another tutorial


<?php
error_reporting(E_ALL);
// we get the IP address of the current user.
$curip = $_SERVER['REMOTE_ADDR'];
//then we do a database query to see if this IP exists
$myarray = array();
if (!in_array ($curip, $myarray)) {
// then we insert the new IP address into the database.
echo "We insert the IP addy: " . $curip . " into the databse";
} else {
echo "The IP addy:" . $curip . " is already in the database.";
}
?>


Here is an introduction to object-oriented programming



<?php
class Bird
{
function __construct($name = 'no-name', $breed = 'breed unknown', $price=15)
{
$this->name = $name;
$this->breed = $breed;
$this->price = $price;
}
}

$aBird = new Bird();
$tweety = new Bird('Tweety', 'canary');

printf("

%s is a %s and costs \$%.2f.

\n", $aBird->name, $aBird->breed, $aBird->price);

printf("

%s is a %s.

\n", $tweety->name, $tweety->breed);
?>

No comments: