PHP


We can use the below script to subtract half an hour from the given time in PHP.

<?php

date_default_timezone_set(‘UTC’);

$actualTime = strtotime(‘2010-07-28 15:00:00′);

$modifiedTime = $actualTime – strtotime(’00:30:00’);

echo “Modified time: ” . date(“H:i a”, $modifiedTime);

?>

Above script gives the output as

Modified time: 14:30 pm

In Mysql InnoDB and MYISAM are two different storage engines. By deafult the storage engine is MYISAM.

Difference between InnoDB and MyISAM :

1. InnoDB supports transactions whereas MYISAM does not. It means we can commit or roll back a transaction in InnoDB but in MYISAM once we execute a command tats it..

2. InnoDB provides row -level locking of a table whereas MYISAM provides only table-level locking.

3. InnoDB supports foreign keys whereas MYISAM does not support foreign key references. Even if you specify foreign key to a table of type MYISAM it willbe simply ignored.

4. Usually we go for InnoDB when we design large size tables and MYISAM is suitable for small size tables

PHP provides two extension modules with which we can connect to Oracle:

  • The normal Oracle functions (ORA); and
  • the Oracle Call-Interface functions (OCI).

OCI is frequently used. ORA doesn’t include support for CLOBs, BLOBs, BFILEs, ROWIDs, etc.

Here is a sample PHP script that uses OCI Extension module to connect Oracle.

<?php

$dbuser = “username”;
$dbpass = “password”;
$dbserver = “server”;
$c=OCILogon($dbuser, $dbpass, $dbserver);
if (!$c) {
$err = OCIError();
echo “Oracle Connect Error ” . $err[text];
}

OCILogoff($c);

?>

The following program fetches data from Oracle database using OCI :

<?php

$dbuser = “username”;

$dbpass = “password”;

$dbserver = “server”;

$c=OCILogon($dbuser, $dbpass, $dbserver);

if (!$c) {

$err = OCIError();

echo “Oracle Connect Error ” . $err[text];

}

$query =”SELECT * from student_details”;

$res= OCIParse($c,$query);

OCI_Execute($res, OCI_DEFAULT);

while(OCIFetch($res)){

$student_id = OCIResult($res, “STUDENT_ID”);

echo “Student Id” . $student_id;

}

OCILogoff($c);

?>

For more information , visit the following url:

http://www.orafaq.com/faqphp.htm#WHAT

PHP provides a very simpler way to fetch the file extension using the function pathinfo() .

The function pathinfo() returns the information about the file path. It takes two arguments .

1. The first argument is the path to be checked.

2. The second argument is optional.It is used to specify which elements are returned.It composes from PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION and PATHINFO_FILENAME. It default returns all elements.

The following example fetches the file extension using pathinfo() :

<?php

$path_parts = pathinfo(‘index.php’);

echo “File Extension : ” . $path_parts[‘extension’];

?>

The output of the above code will be

File Extension : php

In PHP, we can create a log file and write to it, whenever needed. The following is the php file ‘logfile.php’.

<?php
class logfile{
function write($the_string )
{
if( $fh = @fopen( “/logfile.txt”, “a+” ) )
{
fputs( $fh, $the_string, strlen($the_string) );
fclose( $fh );
return( true );
}
else
{
return( false );
}
}
}
?>

In the above example, the class ‘logfile’ has a method ‘write’. This method will write the messages to the log file ‘logfile.txt’. Note that this file should exists before writing to it.

The below file uses ‘logfile.php’ to write the messages to the log file.

require ‘logfile.php’;
$lf = new logfile();
$lf->write(“Writing log file”);
echo “Data written to file”;
$lf->write(“Appending to log file”);

CURL stands for Client URL library functions . Using CURL we can fetch information from other websites.

PHP provides a library called ‘libcurl’ that allows us to connect and communicate to many different types of servers with many different types of protocols.

Currently libcurl supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, and user+password authentication.

In order to use PHP’s CURL functions we need to install the libcurl package.

To fetch information, CURL initialises a session (using the function curl_init()) with the website with which the information has to be fetched . We can set all the options needed for the information transfer (using the function curl_setopt()). After that we can execute the session (using the function curl_exec()) . The function curl_close() is used to close the curl session.

The following example uses CURL to fetch information from the website to a file.

<?php
$ch
= curl_init("http://www.dictionary.com/");
$fp = fopen("example.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

In the above example, the file ‘example.txt’ will contain the information fetched from the given website.

The constant ‘CURLOPT_FILE’ refers to the file with which the information will be fetched. Also the constant ‘CURLOPT_HEADER’ will be set to true if the header is to be included in the output.

There always arises a need for us to convert content from one file format to another one.

To convert or to create PDF files in PHP , we can use the converter HTML2PDF .

HTML2FPDF is a PHP Class library that uses the FPDF class library to convert HTML files to PDF files. This library consist of three classes namely PDF, HTML2FPDF and FPDF (modified FPDF class). The class PDF extends the class HTML2FPDF that extends the class FPDF.

The following example illustrates the conversion of HTML page to PDF file using HTML2FPDF library.

<?php
require(‘html2fpdf.php’);
$pdf=new HTML2FPDF();
$pdf->AddPage();
$fp = fopen(“sample.html”,”r”);
$strContent = fread($fp, filesize(“sample.html”));
fclose($fp);
$pdf->WriteHTML($strContent);
$pdf->Output(“sample.pdf”);
echo “PDF file is generated successfully!”;
?>

First, we need to include the html2fpdf.php file that contains the HTML2FPDF class and an object is created using the constructor HTML2FPDF(). Then a new page is added to the pdf document using the function AddPage(). The html contents are read from the sample.html file using file functions. Then the html contents are written in to the pdf format using WriteHTML() function.

For more information about the HTML2PDF converter , refer to this url

http://www.macronimous.com/resources/Converting_HTML2PDF_using_PHP.asp