Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Friday, April 27, 2018

Fibonacci Series in php

  This program print the fibonacci series.

<?php
//0 1 1 2 3 5 8 13 21 34
  class Fib {
     function printFib($n) {
        if($n==0) return 0;
        $a=0;$b=1;$c;
        echo $a." ".$b ;
        for($i=2;$i<=$n;$i++) {
        $c=$a+$b;
        $a=$b;
        $b=$c;
        echo $c." ";         
        }
     }
  }
 $fib=new Fib();
 $fib->printFib(5);
 ?>

output:

0 11 2 3 5

Thursday, April 26, 2018

Check wether a given string is Palindrome or not

<?php
class Palindrome
{
    public static function isPalindrome($word)
    {
       
    $string = str_replace(' ', '', $word);


    $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string);

    $string = strtolower($string);

    $reverse = strrev($string);

    if ($string == $reverse) {
       return true;
    }
    else {
        return false;
    }
       
    }
}

echo Palindrome::isPalindrome('nitin');
?>

Wednesday, April 25, 2018

Difference between $variable and $$variable.


$variable is a normal variable $$variable takes the value of a variable and treats that as the name of a variable

<?php
 $var="Hello world";
 $$var1="var";
 echo $$var1;
?>
 outPut:
  Hello world
 
  Here:
  $var : Representa a variable.
  $$var1: Represents a variable with content of $var.

Wednesday, April 4, 2018

Print the text on image using GD library in php


GD library is a graphics drawing library that provides tools for manipulating image data. The GD library is used to process images for generating gallery preview and thumbnail size images automatically.

In php GD library provides the function for display the text on the images .
  The given example to show text data on the image using GD library.
 Certificate Image Before

<?php
$yourname = "Brijpal Singh";
$date = "01 Apr 2018";
$pos = "2018";
$rollNo="1003";
$day="Monday";
$month="Apr";
$year="18";
$fta="FTA";
$code="C001";
$startDate="2018";
$endDate="2020";
$dest = imagecreatefrompng('brij.png');//Link  Of Certificate Image
$src = imagecreatefromjpeg('th.jpg'); // Candidate Image (passport size);
imagealphablending($dest, false);

imagecopymerge($dest, $src, 100, 100, 0, 0, 130, 150, 100);
$red = imagecolorallocate($dest, 150,0, 0);
imagefttext($dest, 17, 0, 160, 72, $red, "open-sans/OpenSans-Regular.ttf", $rollNo);
imagefttext($dest, 17, 0, 1080,85, $red, "open-sans/OpenSans-Regular.ttf", $date);
imagefttext($dest, 20, 0, 540, 454, $red, "open-sans/OpenSans-Italic.ttf", $yourname);
imagefttext($dest, 17, 0, 750, 520, $red, "open-sans/OpenSans-Regular.ttf", $pos);
imagefttext($dest, 17, 0, 400, 585, $red, "open-sans/OpenSans-Regular.ttf", $day);
imagefttext($dest, 17, 0, 650, 585, $red, "open-sans/OpenSans-Regular.ttf", $month);
imagefttext($dest, 17, 0, 880, 585, $red, "open-sans/OpenSans-Regular.ttf", $year);
imagefttext($dest, 17, 0, 450, 645, $red, "open-sans/OpenSans-Regular.ttf", $fta);
imagefttext($dest, 17, 0, 880, 645, $red, "open-sans/OpenSans-Regular.ttf", $code);
imagefttext($dest, 17, 0, 500, 705, $red, "open-sans/OpenSans-Regular.ttf", $startDate);
imagefttext($dest, 17, 0, 980, 705, $red, "open-sans/OpenSans-Regular.ttf", $endDate);

// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);

?>

Output:-





Tuesday, April 3, 2018

How to use one PHP class to Another php class


 Here i have create the 3 php files .

MyClass1.php

  <?php
  class MyClass1 {
   public $nm="brij";
}

?>


MyClass2.php
<?php
  class MyClass2 {
   public $nm="brij2";
}

?>

Test.php

<?php
spl_autoload_register(function ($class_name) {
    include $class_name . '.php';
});

$obj  = new MyClass1();
$obj2 = new MyClass2();
echo $obj->nm;
echo $obj2->nm;
?>

Tuesday, December 12, 2017

Change the character set of mysql connection in php


   Using the code we can change the character set of mysql in php for supporting  the utf8 character .


<?php

$mysql_host = 'localhost';
$mysql_username = 'your_database_user_name';
$mysql_password = 'your_password';
$mysql_database = 'your_database_name';
$link = mysqli_connect('localhost', $mysql_username, $mysql_password, $mysql_database);

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

printf("Initial character set: %s\n", mysqli_character_set_name($link));

//change character set to utf8

if (!mysqli_set_charset($link, "utf8")) {
    printf("Error loading character set utf8: %s\n", mysqli_error($link));
    exit();
} else {
    printf("<br><br>character set After Change: %s\n", mysqli_character_set_name($link));
}

mysqli_close($link);
?>


Output of this code :

Initial character set: latin1

character set After Change: utf8 

Monday, October 9, 2017

get list of all file in php project

get list of all file in php project

Sacndir functions are used to get list of file in php project

<?php
$dir    = 'application';
$files1 = scandir($dir);

print_r($files1);

?>


The above example give the following output

Array
(
    [0] => .
    [1] => ..
    [2] => cache
    [3] => config
    [4] => controllers
    [5] => core
    [6] => errors
    [7] => helpers
    [8] => hooks
    [9] => index.html
    [10] => language
    [11] => libraries
    [12] => migrations
    [13] => models
    [14] => third_party
    [15] => views
)


Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...