Tuesday, 30 June 2015

How to check cURL enabled or disabled ?

You can use this script to get CURL status..

<?php
echo 'Curl: ', function_exists('curl_version') ? 'Enabled' : 'Disabled';
?>

What is cURL and how use ?

cURL is a library that lets you make HTTP requests in PHP. Everything you need to know about it (and most other extensions) can be found in the PHP manual.


How to use :-

The first Curl script we are going to look at is the simplest Curl script that is actually useful - it will load a web page, retrieve the contents, then print it out. So, keeping the four-step Curl process in mind, this equates to:

Initialise Curl

Set URL we want to load

Retrieve and print the URL

Close Curl

Here is how that looks in PHP code:

<?php
    $curl = curl_init();
    curl_setopt ($curl, CURLOPT_URL, "http://www.php.net");
    curl_exec ($curl);
    curl_close ($curl);
?> 

How To make sub-string in PHP ?

string substr ( string $string , int $start [, int $length ] )

Returns the portion of string specified by the start and length parameters.

<?php
echo substr('abcdef', 1);     // bcdef
echo substr('abcdef', 1, 3);  // bcd
echo substr('abcdef', 0, 4);  // abcd
echo substr('abcdef', 0, 8);  // abcdef
echo substr('abcdef', -1, 1); // f

?>

Monday, 22 June 2015

PHP Date Function Some days previous

Use the strtotime method provided by PHP.

<?php

echo date('Y-m-d', strtotime('-5 days')); // It shows 5 days previous date

echo date('Y-m-d', strtotime('+5 days')); // It shows 5 days Next date

?>

How To create Constant in PHP ?

Constants are like variables except that once they are defined they cannot be changed or undefined.


<?php

//The example below creates a constant with a case-sensitive name

define("PI", 3.14);

echo PI;


//The example below creates a constant with a case-insensitive name

define("PI", 3.14,true);

echo pi;

?>

What is variable and how to declare in php ?

Variables are "containers" for storing information.

<?php
$str = "Hello world!";
$x = 10;
$y = 15.5;
?>

NOTE : -

the variable $str will hold the value Hello world!
the variable $x will hold the value 10
and the variable $y will hold the value 15.5.




Rules for variable declaration:

A variable starts with the $ sign, followed by the name of the variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive ($age and $AGE are two different variables)

Sunday, 21 June 2015

how to connect to mysql database using pdo in php ?

Connections are established by creating instances of the PDO base class


<?php
try {
$dsn='mysql:host=localhost;dbname=test';
$username='root';
$password='root';

    $con= new PDO($dsn, $username, $password);
 
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

  NOTE : - If there are any connection errors, a PDOException object will be thrown.You may catch the exception if you want to handle the error condition.

PHP Database Connections with MySql

<?php
$con= mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("mysql_database",$con);
echo 'Connected successfully';
mysql_close($con);
?>

localhost at different port like 3307,3308

<?php
$con= mysql_connect('localhost:3308', 'mysql_user', 'mysql_password');
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("mysql_database",$con);
echo 'Connected successfully';
mysql_close($con);
?>

OR

<?php
$con= mysql_connect('127.0.0.1:3308', 'mysql_user', 'mysql_password');
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("mysql_database",$con);
echo 'Connected successfully';
mysql_close($con);
?>

How to get first and last date of current month ?

<?php
$first_day_this_month = date('Y-m-01');
$last_day_this_month  = date('Y-m-t');
echo "First Date Of current Month - ".$first_day_this_month."</br>";
echo "First Date Of current Month - ".$last_day_this_month."</br>";
?>

How To get First Date And Last Date of current Week ?

<?php

if(date('D')!='Mon')
{  
//take the last monday
$staticstart = date('Y-m-d',strtotime('last Monday'));
}else{
$staticstart = date('Y-m-d');  
}
if(date('D')!='Sat')
{
$staticfinish = date('Y-m-d',strtotime('next Saturday'));
}else{
$staticfinish = date('Y-m-d');
}
echo "First Date of Current week - ".$staticstart."</br>";
echo "Last Date of Current week - ".$staticfinish."</br>";

?>

how to get difference between two dates in php

<?php 
$date1="2011-01-02";
$date2=date("Y-m-d");

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);

?>


O/P -  4 years, 5 months, 20 days


Saturday, 20 June 2015

What is PHP

The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications.

"Hello World" Script in PHP

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'?> 
 </body>
</html>