Hi friends , now have a look at my custom functions tutorial.
QUICK DEFINITIONS:
VARIABLES:
1.have a temporary value
2.must not start with digits/numbers
3.can be any value , numeric , chars
4.starts with a dollar sign
5.USAGE: $a="hello";echo $a; output:hello
CONSTANTS:
1.have permanent value
2.must not start with digits/numbers
3.can be any value , numeric , chars
5.USAGE: system('a'="hello');echo a; output:hello
ARRAYS
1.STORES IN A VARIABLE USUALLY
2.HAVE MULTIPLE VALUES
3.VALUES ARE TEMPORARY
4.USAGE: $a=array(); $a[0]="h";$a[1]="e";$a[3]="ll";$a[4]="0"; echo $a[1]; output:h
5.WISE USAGE: for($y=0;$a[$y]!=null;$y++){$c=$y-1;echo $a[$c];} output: hello
6:THERE ARE 3 TYPES OF ARRAYS IN PHP,
a.normal array
b.associative array
c.multidimensional array
ASSOCIATIVE ARRAYS:
4.USAGE: $a=array(); $a[a]="h";$a[-b]="e";$a[c]="ll";$a[d]="0"; echo $a[a]; output:h
SAME AS NORMAL ARRAYS BUT THE ARRAY KEY , THE ONE IN BRACKETS ([1]) is a char.
MULTIDIMENSIONAL ARRAYS:
I'll come to you with this later...now only these are needed.
OPERATORS:
== equals
if(1==1){//dosomething}
!= notequals
if(1!=1){//dosomething}
or
if(1=="1"or(a==1)){//dosomething}
|| same as or
if(1=="1"||a==1){//dosomething}
&& and
if(1=="1"&&(a==1)){//dosomething}
and and
if(1=="1"and(a==1)){//dosomething}
++
value+1 , example : $c=1;$c++;echo $c; output:2
--
value-1 , example : $c=2;$c--;echo $c; output:1
-
MINUS example : $c=2; $b=1; $d=$c-$b; echo $d; output:1
+
PLUS example : $c=2; $b=1; $d=$c+$b; echo $d; output:3
*
INTO example : $c=2; $b=1; $d=$c*$b; echo $d; output:2
/
DIVIDE example : $c=2; $b=1; $d=$c/$b; echo $d; output:2
%
MODULUS - DIVIDE ONE VALUE BY OTHER AND RETURN THE REMINDER:
example : $c=2; $b=1; $d=$c%$b; echo $d; output:0 or null
===
IDENTICAL TO
!===
NOT IDENTICAL TO
<
less than
>greater than
<=
less than or equal to
>=
greater than or equal to
!
NOT
Xor
checks if only one of two statements are true
+=
ADD one value to other
example: $a="1";$a+=1; echo $a; output:2
-=
SUB'S one value to other
example: $a="2";$a-=1; echo $a; output:1
*=
MULTIPLIES one value to other
example: $a="1";$a*=1; echo $a; output:1
/=
DIVIDES one value to other
example: $a="1";$a+/1; echo $a; output:1
.=
COMBINES TWO VALUES
example $a="he"; $a.="llo"; echo $a; output:hello
-------
+=
ADD one value to other
example: $a="1";$a+=1; echo $a; output:2
Creating functions
----
*BASIC STATEMENTS:
Php supports custom functions.
These functions are executed in the script itself and doesent store in header files...etc..
These functions are just defined in a PHP script.
Functions cant process outside variables/constants but can be done with the global keyword.
Functions can return a value.
Functions are the simplest way to work a task though OOP is available.
You can create a function with the function keyword and the function name.
Code:
<?
//Creating functions , naveen.
function myfunction()
{
//function programs-statements...
}
?> Every function must end with a '
)'.
Whether it is a custom defined function or supplied function.
After you create a function , function statements must be created in the '{}' braces.
Test a function.
Code:
<?
//My first created function.
function sample()
{
$a="Hello";
$b="{$a} World!";
$c=$b;
echo $c;
}
//test the function
sample();
?>
##########OUTPUT##########
Hello World!
#########################
Now you know to create a function.
Now lets pass arguments to our new function.
---
Now have a deep look at this program...
Code:
<?
//Test a var function.
$test="Hello World!";
function sample()
{
echo $test;
}
sample();
unset($test);
?> You would have thought this would print the value of $test variable,
As acidburn thought u all about variables , whats a varvalue, constant etc...
but this THIS WILL NOT PRINT THE VALUE OF $test .
Why?
come'on man , i'll explain it howdy...
first we'll have a look what it'll gonna echo to the browser...
Code:
#########OUTPUT#########
#######################
Huh? wots wrong? there's nothing!
Yeah cuz sample() doesnt know wot $test is.
Thik that $test is in your house and ur neighbour dosent know about it.
Then what would ur poor neighbour do? nothing right? cuz he doesent know anything abt it...
Now lets tell our neighbour about $test with our global keyword.
Code:
<?
//Test a var function.
$test="Hello World!";
function sample()
{
global $test;
echo $test;
}
sample();
unset($test);
?>
###########OUTPUT#######
Hello World!
####################### Yeah , now global keyword told our neighbour about $test and they printed value of it.
Just remember when the script is executed , the var and its values are remembered till the entire script
is executed.When entire script is executed , $test is remembered global enables the var name and value
of $test.
Echo prints $test.
Thus , Hello World! is displayed.
Unset??
yeah , unset() is a function to delete a variable.
Try this ,
Code:
<?
//Test a var function.
$test="Hello World!";
function sample()
{
global $test;
echo $test;
}
unset($test);
sample();
?> Nothing will be printed cuz unset deleted $test before sample() was executed.
These readymade functions are stored in php's header files,
you can even create one yourself.
For example strlen() functions prints the string length.
Code:
<?
//My string length function usage
$a="Hello";
//test string
function teststring()
{
global $a;
$b=strlen($a);
echo "The string '{$a}' contains {$b} chars.";
}
teststring();
?>
########OUTPUT#########
The string 'Hello' contains 5 chars.
###################### Although you can create it urself.
Code:
<?
$a="Hello";
function teststring()
{
global $a;
//test each char in array $a
for($y=0;$a[$y]!=null;$y++)
{
}
//Now ,
$stringlength=$y;
echo "The string '{$a}' contains {$stringlength} chars.";
}
teststring();
?> First $a is denoted a value "Hello",
Then function teststring is defined.
Now , global has been done to know what $a is.
Now through loop , $y has been defined as zero , as $a is an array of chars , array of $a with $y should
not be null , if it is , loop is ended , if not $y is added once.
This loop goes on until $y's value is 0 or null.
Now as meaningful var rule , $stringlength has been duplicated as $y.
Now , its printed in a meaningful manner.
Thus , we have created our new custom teststring function.
-----
Now lets go to arguments.
Each and everytime we cant denote a var value and retrive it through global,
There's an easy and usefull way to do that and it is called as ARGUMENTS.
Code:
<?
//my first argument function
function sample($a)
{
if(isset($a))
{
echo "Your argument was {$a}";
}
else
{
echo "Argument value is null";
}
//end function
}
sample('Hello');
?>
#########OUTPUT#######
Your argument was Hello
##################### Now i'll explain it.
First we create a function called 'sample'.
Now , we place the argument value var as $a.
Any arguments passed (ex. sample('abc'))
will b stored in $a variable.
Now when we print a's value , the argument stored in that variable is printed.
Now lets oppsitely test it...
Code:
<?
//my first argument function
function sample($a)
{
if(isset($a))
{
echo "Your argument was {$a}";
}
else
{
echo "Argument value is null";
}
//end function
}
sample();
?>
Now if you have E_WARNING:on in the php.ini file ,
the output would be something like this...
########OUTPUT#########
Warning: Missing argument 1 for sample() in /ur directories/sample.php on line 3
Argument value is null
######################
if it was E_WARNING:off in the php.ini file ,
then the output would be..
########OUTPUT#########
Argument value is null
######################
Congrats! you have created your first argument function!
----Return a value from a function----
Code:
<?
//create a return function
function sample($a)
{
//test argument a is a set or not
if(isset($a))
{
//if yes, assign b as..
$b="Returned ! , value is {$a} ";
}
else
{
//assign b as...
$b="Returned !, value is null.";
}
//return b
return $b;
}
//store function return in c
$c=sample('hello');
//print c
echo "$c";
?>
########OUTPUT########
Returned!,value is Hello
##################### Return returns the value/data if a variable/constant/function.
Now the value of $a will be stored in $b.
As a result , Hello will be echod.
Lets test it in another way...
Code:
<?
//create a return function
function sample($a)
{
//test argument a is a set or not
if(isset($a))
{
//if yes, assign b as..
$b="Returned ! , value is {$a} ";
}
else
{
//assign b as...
$b="Returned , value is null.";
}
//return b
return $b;
}
//store function return in c
$c=sample();
//print c
print "$c";
?>
Now if you have E_WARNINGS:on in the php.ini file ,
the output would be something like this...
##########OUTPUT##########
Warning: Missing argument 1 for sample() in URDIRECTORIES/sample.php on line 2
Returned , value is null.
#########################
Now , as far as u have learnt , we can create an all time string length tool.
Code:
<?
<?
$b=$_GET['word'];
$type=$_GET['type'];
//create a proper value
$a=strtolower($b);
function teststring($string)
{
//test each char in array $a
for($y=0;$string[$y]!=null;$y++)
{
}
//Now ,
$stringlength=$y;
echo "The string '{$string}' contains {$stringlength} chars.-TESTSTRING METHOD";
}
if(isset($a,$type))
{
if($type=="teststring")
{
teststring($a);
}
elseif($type=="strlen")
{
$b=strlen($a);
echo "The string '{$a}' has {$b} chars.-STRLEN METHOD";
}
else
{
echo "INCORRECT METHOD!
Correct methods:
1.strlen
2.teststring
";
}
}
else
{
echo"Please specify all details! You have not specified the word or the type or both.";
}
?> Now we have created our strlength finder script.
Case can be capital or small or both , it'll be converted to lower with strtolower();
It will run with a BOOLEAN statement.
Functions are made.
Value is given from GET METHOD.
Now to test this , point this_filename.php?word=wordname&type=urtype .
Lets move on...
------------------
TIP: If you are creating a complex web/wap applications,
Then u have to define many functions in a specific file and then have to include
for furthor use.
k:
------------------
Next...
-BASIC PHP
-ARRAYS
-WORKING WITH WML
-FILESYSTEM
-STRINGS
-DATABASE-MySQL
Should i continue with these?
Please post / pm me ANYONE , or GUMSLONE....
#########ANY ERRORS / BUGS / COMMENTS CAN BE POSTED OR PM'D##############
Thanks,
naveen.
Bookmarks