15 Daily use PHP Functions


  Mysql connection

<?php 


servername = "localhost";
$username = "root";
$password = "123xyz";
$dbname = "database";

// Create connection
global $conn;
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$conect = mysqli_connect($servername, $username, $password, $dbname) or die(mysql_error());


?>





PHP misclenious function:


1.  define() :    Defines a constant

Example : <?php
define("GREETING","Hello you! How are you today?");
echo constant("GREETING");
?>



2. sleep() : function delays execution of the current script for a specified number of seconds.

Example : <?php
echo date('h:i:s') . "<br>";

//sleep for 5 seconds
sleep(5);

//start again
echo date('h:i:s');
?>


Math function ......

3. floor() :     Rounds a number down to the nearest integer
Example :
   <?php
echo(floor(0.60) . "<br>");
echo(floor(0.40) . "<br>");
echo(floor(5) . "<br>");
echo(floor(5.1) . "<br>");
echo(floor(-5.1) . "<br>");
echo(floor(-5.9));
?>

Output : 0
0
5
5
-6
-6

4. max() :     Returns the highest value in an array, or the highest value of several specified values
Example :
<?php
echo(max(2,4,6,8,10) . "<br>");
echo(max(22,14,68,18,15) . "<br>");
echo(max(array(4,6,8,10)) . "<br>");
echo(max(array(44,16,81,12)));
?>
Output : 10
68
10
81

5.min()     Returns the lowest value in an array, or the lowest value of several specified values
Example :
<?php
echo(min(2,4,6,8,10) . "<br>");
echo(min(22,14,68,18,15) . "<br>");
echo(min(array(4,6,8,10)) . "<br>");
echo(min(array(44,16,81,12)));
?>
Output :
2
14
4
12


6...... rand() Generates a random integer
Example:<?php
echo(rand() . "<br>");
echo(rand() . "<br>");
echo(rand(10,100));
?>
Output :
29400
28791
96


7..... round()     Rounds a floating-point number

<?php
echo(round(0.60) . "<br>");
echo(round(0.50) . "<br>");
echo(round(0.49) . "<br>");
echo(round(-4.40) . "<br>");
echo(round(-4.60));
?>

output :
 1
1
0
-4
-5


8..... pow()     Returns x raised to the power of y

<?php
echo(pow(2,4) . "<br>");
echo(pow(-2,4) . "<br>");
echo(pow(-2,-4) . "<br>");
echo(pow(-2,-3.2));
?>

Output :
16
16
0.0625
NAN


9........abs()     Returns the absolute (positive) value of a number
<?php
echo(abs(6.7) . "<br>");
echo(abs(-6.7) . "<br>");
echo(abs(-3) . "<br>");
echo(abs(3));
?>

Output :
6.7
6.7
3
3

Some Array Function

10...... array_push()     Inserts one or more elements to the end of an array

Example : <?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>

11. array_merge()     Merges one or more arrays into one array

Example :  <?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
?>
Output :


12. array()     Creates an array

Example:<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

13. in_array()     Checks if a specified value exists in an array

Example : <?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
if (in_array("Glenn", $people))
  {  echo "Match found";  }
else
  {  echo "Match not found";  }
?>

Output :Match found


14. sizeof()     Alias of count()

Example: <?php
$cars=array("Volvo","BMW","Toyota");
echo sizeof($cars);
?>

Output :3


15. count()     Returns the number of elements in an array
Example : <?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>

Output : 3

No comments:

Post a Comment

Pages