Basic PHP
eharetea

PHP primer.

Just the basics.

Date Created:Friday December 29th, 2006 03:41 AM
Date Modified:Wednesday July 30th, 2008 05:50 PM

# sets $var to hello
$var = hello;

# sets $Var to reference $var
# two variables now will have the same value
$Var = &$var;

# construct variable by concatenating two variables
<?
$var = "foo";
$Var = "bar";
${$var . $Var} = "value";
echo ${$var . $Var};
?>

$_GET
Variables accessed via URL query string. ie: http://www.site.com/index.php?C=1
echo $_GET['C'];

$_POST
Variables accessed via POST.

$_SERVER['PHP_SELF']
current php page

GLOBALS:
script a
<?
$a = 1;
include 'b.inc';
?>

script b
# global is not the best method
<?
function test()
{
echo global $a;
// echo $GLOBALS['a'];
// both methods work. 
}
Test();
?>

script b
# this is more suitable:
<?
function test($arg)
{
echo $arg;
}
Test($a);
?>

VARIABLE VARIABLES
<?
$a = 'hello';
$$a = 'world';
echo $a ${$a};
// echo $a $hello;
?>

<?
$a = "hello world";
$b = "a";
print $$b;
?>

VARIABLES WITH CONSTANTS
<?
define("TEST","value");
$value = "foobar";
echo ${TEST};
?>

EXTRACT FROM ARRAY
<?
$array =array("one" => "First Value",
 "two" => "Second Value"
);
extract( $array, EXTR_PREFIX_ALL, "new_prefix_");
print $one_new_prefix;
print $two_new_prefix;
?>

USING FORMS AND POST
<form action="<?echo $_SERVER['PHP_SELF']; ?>" method="post">
Name: <input type="text" name="username" />
<input type="submit" name="submit" value="Submit!" />

then use:
echo $_POST['username'];
or
echo $_REQUEST['username'];
// $_REQUEST looks at $_[COOKIE,GET,POST]
// order of priority is in php.ini

<input type="image" src="image.gif" name="imagevar" />
//the coords of where the user clicks will be stored in $imagevar_x and $imagevar_y

SETCOOKIE
<?
if (isset($_COOKIE['count'])) {
	$count = $_COOKIE['count'] + 1;
} else {
	$count = 1;
)
setcookie('count', $count, time()+3600);
setcookie("Cart[$count]", $item, time()+3600);
?>

PARSING ARGUMENTS
func_num_args() function
<?
function countargs()
{
	$numargs = func_num_args();
	echo "\$numargs: $numargs\n";
}
countargs(1, 2, 3, 4); //returns '$numargs: 4'
?>

func_get_arg() function
<?
function showargs($a)
{
	$numargs = func_num_args();
	for ($i = 0; $i < $numargs; $i++) {
		$arg = func_get_arg($i);
		if (! is_null($arg)) {
			return $arg;
		}
	}
	return null;
}

echo showargs(1, 2, 3, 4);
?>

func_get_args() function
set using
$arg_list = func_get_args()
call using
echo $arg_list[$i]

DEFINING CONSTANTS
<?
define("TEST","hello world");
echo TEST; //echos hello world
?>

TESTING FILE EXISTENCE
<?
$filename = '/home/3d/hscript.txt
if (file_exists($filename)) {
	echo "exists";
} else {
	echo "does not exist";
}
?>


PREDEFINED "MAGIC" CONSTANTS
__LINE__  current line number of file
__FILE__  path of current file; if used in include, returns inlude path
__FUNCTION__  the function name
__CLASS__  the class name
__METHOD__  the class method name

echo __METHOD__;


PHP:

for ( $i = 0 ; $i <= 10 ; $i++ )
{
   $x = 2;
   $y = $z/10;
   $z = $i*$i;

   print $x . "," . $y .",". $z . "\n";


}
for ($v = 0 ; $v <= 9; $v++ )
{

   $velocity = 2;
   $b = 3+$v;
   $c = $d-$velocity;
   $d = 4*$v;

   print $velocity . "," . $b .",". $c . "\n";


}
{
for ( $r = 0 ; $r <= 100 ; $r++ )

   $you = 20*$r;
   $test = $you;
   $sir = ($text)*(abs(sin($r)));

   print "PHP Kicks Ass";
   print "\n";
   print $sir;
}



Downloads:
Download: php.txt 3 KB
Download: phpnums.txt 479 B

Please login or Click Here to register for downloads
Creative Commons License
Basic PHP by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com