We can get the first element of an array by the function current();
<?php
$arr = array('name'=>'angel','age'=>'23','city'=>'delhi','profession'=>'php developer');
$firstvalue = current($arr);
?>
OUTPUT : angel
<?php
$a=15;
$b=10;
echo "The value of a is ".$a." and b is ".$b;
echo " before swapping. ";
$temp=$a;
$a=$b;
$b=$temp;
echo "The value of a is ".$a." and b is ".$b;
echo " After swapping ";
?>
OUTPUT: The value of a is 5 and b is 10 before swapping.
The value of a is 10 and b is 5 After swapping
<?php
$numbers=array(12,23,45,20,5,6,34,17,9,56);
$length=count($numbers);
$min=$numbers[0];
for($i=1;$i<$length;$i++)
{
if($numbers[$i]<$min)
{
$min=$numbers[$i];
}
}
echo "The smallest number is ".$min;
?>
OUTPUT: The smallest number is 5.
<html>
<head>
<title> Number of times the web page has been viited.</title>
</head>
<body>
<?php
session_start();
if(isset($_SESSION['count']))
$_SESSION['count']=$_SESSION['count']+1;
else
$_SESSION['count']=1;
echo "<h3>This page is accessed</h3>".$_SESSION['count'];
?>
</body>
</html>
<?php
echo php_uname()."\n";
echo PHP_OS."\n";
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
echo 'This is a server using Windows!';
} else {
echo 'This is a server not using Windows!'."\n";
}
?>
<?php
class factorial_of_a_number
{
protected $_n;
public function __construct($n)
{
if (!is_int($n))
{
throw new InvalidArgumentException('Not a number or missing argument');
}
$this->_n = $n;
}
public function result()
{
$factorial = 1;
for ($i = 1; $i <= $this->_n; $i++)
{
$factorial *= $i;
}
return $factorial;
}
}
$newfactorial = New factorial_of_a_number(5);
echo $newfactorial->result();
?>
<?php
class user_message {
public $message = 'Hello All, I am ';
public function introduce($name)
{
return $this->message.$name;
}
}
$mymessage = New user_message();
echo $mymessage->introduce('Scott')."\n";
?>
Two strings can be joined together by the use of a process called as concatenation. A dot (.) operator is used for this purpose.
<?php
$string1 = Hello;
$string2 = World;
$stringall = $string1.$string2;
echo $stringall;
?>
There is no special way to set persistent cookies. Setting Cookies with an expiration date become persistent cookie.
setcookie( "cookieName", 'cookieValue', strtotime( '+30 days' ) );