Search This Blog

Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

function with return in php

<?php

$num1=10;
$num2=5;

function add($num1,$num2)
{
return $num1+$num2;
}


function multi($num1,$num2)
{
return $num1*$num2;
}

echo 'add : '.add($num1,$num2).'<br>';

echo 'multiply : '.multi($num1,$num2).'<br>';

echo 'function in function : '.add(multi($num1,$num2),multi($num1,$num2)).'<br>';


?>

output :

add : 15
multiply : 50
function in function : 100

global variable in php

<?php

$num1=10;
$num2=5;

function add()
{
  global $num1,$num2;
  return $num1+$num2;
}

function sub($num1,$num2)
{
  return $num1-$num2;
}

echo 'addition  with out global variables : '. add().'<br>';

echo 'subtraction with global variables : '.sub($num1,$num2);

?>output :

addition with out global variables : 15
subtraction with global variables : 5

function with parameters in php

<?php
 
function hai($age){

if($age==21)
{
  echo 'you are eligible for vote';
}
else{
  echo 'you are not eligible for vote';
}
}
$age=21;
echo hai($age);
?>
outputt :

you are eligible for vote

simple function in php

<?php

function hai(){

echo 'hello world';

}
echo hai().', i am a human';
?>

output :

hello world, i am a human

note  :  " . "  use for concatenation

die() and exit() Functions in php

<?php

@mysql_connect('localhost','root','') or die("not connect");


echo 'connected';

?>

outout  : connected
 
<?php


@mysql_connect('localhost','root','n') or die("not connect");

echo 'connected';

?>

outout  : not connect

<?php

//   " @ " use for errors from php not display

@mysql_connect('localhost','root','') or exit("not connect");

echo 'connected';

?>

outout  : connected

<?php


@mysql_connect('localhost','root','n') or exit("not connect");

echo 'connected';

?>

outout  : not connect

note : 1)     can use " || " in the place of " or " 
         2)    " @ " use for errors not display from php

special data types in php

                                     resource data types and null data types




resource data types refers the external resources  like data connection , file pointer , FTP connection ...ect


<?php

mysql_connect("hostname",username","password");

?>

<?php

$con=mysql_connect("hostname",username","password");
echo $con;

?>

here con is external resource type

output :    resource #id2
                                                                  null data type



null is not a value

the variable not initialized with any  value
the variable initialized with null value
if the value of the variable is removed using unset function

based on by that 3 conditions we consider a variable has null value


<?php

$a=100;
unset($a);
echo is_null($a);

?>

output :  1

if is it null returns 1 else returns nothing




Share Links

Related Posts Plugin for WordPress, Blogger...