Friday, December 24, 2010

What is Type Juggling in php

Type Juggling in php:

PHP does not require explicit type definition in variable declaration. a variable's type is determined by the context in which that variable is used. That is, if you assign a string value to variable $var, $var becomes a string. If you then assign an integer value to $var, it becomes an integer.

An example of PHP's automatic type conversion is the addition operator '+'. If any of the operands is a float, then all operands are evaluated as floats, and the result will be a float. Otherwise, the operands will be interpreted as integers, and the result will also be an integer. Note that this does NOT change the types of the operands themselves; the only change is in how the operands are evaluated.

$foo += 2; // $foo is now an integer (2)
$foo = $foo + 1.3; // $foo is now a float (3.3)
$foo = 5 + "10 Little Piggies"; // $foo is integer (15)
$foo = 5 + "10 Small Pigs"; // $foo is integer (15)

If the last two examples above seem odd, see String conversion to numbers.
If you wish to change the type of a variable, see settype().
If you would like to test any of the examples in this section, you can use the var_dump() function.
Note: The behavior of an automatic conversion to array is currently undefined

What are the functions for IMAP?

Internet Message Access Protocol:

imap_body : Read the message body
imap_check : Check current mailbox
imap_delete : Mark a message for deletion from current mailbox
imap_mail : Send an email message

Friday, November 19, 2010

What are the database space-saving functions available in php ?

# Use ip2long() and long2ip() to store the IP adresses as Integers instead
of storing them as strings, which will reduce the space from 15 bytes to 4
bytes. This will also increase search speed and make it easy to see if a ip
falls within a specified range.

# Use gzcompress() and gzuncompress() to reduce the strings before you
store them in a database. The gzcompress can compress plain-text up to 90%.
The only reason why you shouldn’t use it is when you need full-text
indexing capabilities.

How can we save an image from a remote web server to my web server using PHP?

$file_rimg = fopen("http://w3answers /image23.jpg",'rb');
$newfile_name_img = "/tmp/tutorial.file";
$file_wnew = fopen($newfile_name_img,'wb');
while (!feof($file_rimg)) {
$chunk_rd = fread($file_rimg,1024);
fwrite($file_wnew,$chunk_rd);
}
fclose($file_wnew);
fclose(file_rimg);

Is Single Quotes better then double quotes

By using single quotes instead of double quotes, we save PHP from having
to parse your string for variables. It's not only is faster, but I find it
more programmer-friendly because it is easier to find variables in your
code.
Also, when referencing an array that has a string index, always use single
quotes. This prevents PHP from having to figure out exactly what you were
trying to say.

Saturday, October 30, 2010

How To Protect Special Characters in Query String?

If you want to include special characters like spaces in the query string,
you need to

protect them by applying the urlencode() translation function. The script
below shows

how to use urlencode():


print(“< html >”);
print(“< p >
Please click the links below”
.” to submit comments about FYICenter.com:< /p >
”);
$comment = ‘I want to say: “It\’s a good site! :->”‘;
$comment = urlencode($comment);
print(“< p >

.”
.”It’s an excellent site!
< /p >
”);
$comment = ‘This visitor said: “It\’s an average site! :-(“‘;
$comment = urlencode($comment);
print(“< p >

.’
.”It’s an average site.

”);
print(“< /html >”);

Advantages and disadvantages of CASCADE STYLE SHEETS?

External Style Sheets
Advantages
Can control styles for multiple documents at once Classes can be created
for use on

multiple HTML element types in many documents Selector and grouping
methods can be

used to apply styles under complex contexts

Disadvantages
An extra download is required to import style information for each
document The

rendering of the document may be delayed until the external style sheet is
loaded

Becomes slightly unwieldy for small quantities of style definitions

Embedded Style Sheets
Advantages
Classes can be created for use on multiple tag types in the document
Selector and

grouping methods can be used to apply styles under complex contexts No
additional

downloads necessary to receive style information

Disadvantage
This method can not control styles for multiple documents at once

Inline Styles
Advantages
Useful for small quantities of style definitions Can override other style

specification methods at the local level so only exceptions need to be
listed in

conjunction with other style methods

Disadvantages
Does not distance style information from content (a main goal of
SGML/HTML) Can not

control styles for multiple documents at once Author can not create or
control classes

of elements to control multiple element types within the document Selector
grouping

methods can not be used to create complex element addressing scenarios

Wednesday, September 29, 2010

How To Read the Entire File into a Single String?

Read the Entire File into a Single String
If you have a file, and you want to read the entire file into a single
string, you can use the file_get_contents() function. It opens the
specified file, reads all characters in the file, and returns them in a
single string. Here is a PHP script example on how to file_get_contents():


$file = file_get_contents("/windows/system32/drivers/etc/services");
print("Size of the file: ".strlen($file)."\n");


This script will print:

Size of the file: 7116

Wednesday, August 18, 2010

Hide .php extension or Hide PHP Usage using .htaccess

If You may not want other people to know that you are using PHP programming... Here is a easy ways to hide the fact that you're using PHP.
Use another extension than .php for your PHP files... your can use .foo, .blabla, .asp or even .htm and .html (in the later case, PHP parses all the files ending in .html, but it will work fine of course if some of these files are plain HTML; slowdown for parsing plain HTML is not noticeable even on the highest loads thanks to PHP's cache).
This process can be done easily by adding the following line to your .htaccess file in the same directory than your code:
AddType application/x-httpd-php .foo .blabla .asp .htm .html

Send html mail with PHP

$to = 'pleace your mail id here';
$subject =’Place your subject here’;
$random_hash = md5(date('r', time()));
$headers = 'From: '.$my_fname.' '.$my_lname.' ' ."< ".$my_email." >". "\r\n";
$headers .= "Reply-To: ". $my_email. "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$massage='|table width="100%" border="0" align="center" cellpadding="0" cellspacing="1"|
|tr||td|Title :|/td||td|'.$my_title.'|/td||/tr|
|tr||td|First Name :|/td||td|'.$my_fname.'|/td||/tr|
|tr||td|Last Name :|/td||td|'.$my_lname.'|/td||/tr|
|/table|
’;
@mail( $to, $subject, $massage, $headers );

How to Ignore User Abort ?

When your script is doing something sensible work (like database updating etc.) and you absolutely don't want it to be interrupted by the user pressing the 'Stop' button of his browser. Then just add the following code to your script:

ignore_user_abort(true);

How to show PHP credits?.

Adding ?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000 at the end of the URL of PHP scripts will expose the PHP credits.

Create an image on the fly with the GD library

Run the following code to create an image on the fly:
header ("Content-type: image/png");
$im = @imagecreate (150, 50)
or die ("Sorry Couldn't create image!");
$background_color = imagecolorallocate ($im, 205, 205, 255);
$text_color = imagecolorallocate ($im, 233, 14, 91);
imagestring ($im, 2, 5, 5, "Your demo text goes here", $text_color);
imagepng ($im);
imagedestroy($im);

How to Save bandwidth

To save bandwidth add the following code at the beginning of your PHP page, and then PHP will automatically compress the page to web browsers that support with feature (support in both Internet Explorer and Mozilla Firefox do).
Since HTML can easily be compressed, you can expect to cut down by at least 2/3 the bandwidth used at the expense of some extra load on the server's CPU, but the CPU load difference usually isn't even noticeable.

@ini_set('zlib.output_compression_level', 1);
@ob_start('ob_gzhandler');

Saturday, July 31, 2010

What is CAPTCHA?

CAPTCHA stands for Completely Automated Public Turing Test to tell Computers and Humans Apart. CAPTCHA use for prevent spammers from using bots to automatically fill out forms, CAPTCHA programmers will generate an image containing distorted images of a string of numbers and letters. Computers or bot cannot determine what the numbers and letters are from the image but humans have great pattern recognition abilities and will be able to fairly accurately determine the string of numbers and letters. By entering the numbers and letters from the image in the validation field, the application can be fairly assured that there is a human client using it.
You can get more info here:
http://en.wikipedia.org/wiki/Captcha

Tuesday, June 29, 2010

What are the advantages of stored procedures, triggers, indexes?

Stored procedure : Stored procedureis a set of SQL commands that can be compiled and stored in the server. Once this process has been done, user don’t need to keep re-issuing the entire query he can refer to the stored procedure. This provides better overall performance because the query has to be parsed only once and less information needs to be sent between the server and the user. We can also raise the conceptual level by having libraries of functions in the server. However, stored procedures of course do increase the load on the database server system, as more of the work is done on the server side and less on the client (application) side.

Trigger : A trigger is effectively a type of stored procedure, one that is invoked when a particular event occurs. For example, we can install a stored procedure that is triggered each time a record is deleted from a transaction table and that stored procedure automatically deletes the corresponding customer from a customer table when all his transactions are deleted.

Indexes : Indexes are used to find rows with specific column values quickly. Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows. The larger the table, the more this costs. If the table has an index for the columns in question, MySQL can quickly determine the position to seek to in the middle of the data file without having to look at all the data. If a table has 1,000 rows, this is at least 100 times faster than reading sequentially. If you need to access most of the rows, it is faster to read sequentially, because this minimizes disk seeks.

Saturday, May 15, 2010

How to Extract Domain name from URL ?

Here is a simple example describe below.

“parse_url” function can be used to parse the URL.

parse_url is a built in php function which can be used to parse url and get the type of request like whether its http or https or ftp, gives the domain name like www.example.com and path and the query.

eg. consider this url – “http://www.example.com/page.php?id=1”

In “http://www.example.com/page.php?id=1” this url if you execute parse_url function then it will show the following result.

Scheme - http
url - www.example.com
path - /page.php
query - id=1

Monday, April 12, 2010

How to Unlink all files and folder in a particular folder?

Basically we use "unlink" to delete a file physically.
The syntax is unlink(file path including file name).

But if we want to delete all files and folder of a perticular folder then we can use this code

// arguments : The target directory , delete directory or leave it blank (true/false)
// return : none

function removeAll ($dir, $delDir) {
if(!$dh = @opendir($dir)) return;
while (false !== ($obj = readdir($dh))) {
if($obj=='.' || $obj=='..') continue;
if (!@unlink($dir.'/'.$obj)) removeAll ($dir.'/'.$obj, true);
}
if ($delDir){
closedir($dh);
@rmdir($dir);
}
}

Note: Please change folder permission (0777) before excuting the code

Sunday, March 21, 2010

What is Crawler and Indexer

Crawler and Indexer

Typically the crawler is the component of the search engine responsible for going out and retrieving
content for the indexer to catalog. It reads the list of addresses from the database and downloads a copy
of each document to queue locally to disk where the indexer can access them. Then the indexer
component processes each file in the queue. This tag - team approach works well for large search sites
with massive amounts of data continuously being indexed or if the crawler scans through the documents
in search of links to other documents to retrieve (as is the case with recursive downloading/leeching).

Sunday, February 28, 2010

Difference between section and foreach loop in Smarty

The main difference between SECTION and FOREACH is that

1.For SECTION you can start from a specific value, and can also set a step for the iteration, whereas for FOREACH you have to loop over all values.

2.foreach is used for associative arrays

Thursday, February 25, 2010

Array Functions : How to insert an element or remove an element from an array

In this process you can insert an element or remove an element from an array


$numbers = array(1,2,3,4,5,6);
print_r($numbers);
echo "

";

// shifts first element out of an array
// and returns it.
$a = array_shift($numbers);
echo "a:" . $a ."
";
print_r($numbers);
echo "

";

// prepends an element to an array,
// returns the element count.
$b = array_unshift($numbers, 'first');
echo "b: ". $b ."
";
print_r($numbers);
echo "

";

echo "
";

// pops last element out of an array
// and returns it.
$a = array_pop($numbers);
echo "a: " . $a ."
";
print_r($numbers);
echo "

";

// pushes an element onto the end of an array,
// returns the element count.
$b = array_push($numbers, 'last');
echo "b: ". $b ."
";
print_r($numbers);
echo "

";

Friday, February 5, 2010

Import mysql data to excel

// Connect database.
mysql_connect("localhost","","");
mysql_select_db("import");

// Get data records from table.
$result=mysql_query("select * from name_list order by id asc");

// Functions for export to excel.
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=orderlist.xls ");
header("Content-Transfer-Encoding: binary ");

xlsBOF();

/*
Make a top line on your excel sheet at line 1 (starting at 0).
The first number is the row number and the second number is the column, both are start at '0'
*/

xlsWriteLabel(0,0,"List of car company.");

// Make column labels. (at line 3)
xlsWriteLabel(2,0,"No.");
xlsWriteLabel(2,1,"Company");

$xlsRow = 3;

// Put data records from mysql by while loop.
while($row=mysql_fetch_array($result)){

xlsWriteNumber($xlsRow,0,$row['id']);
xlsWriteLabel($xlsRow,1,$row['name']);

$xlsRow++;
}
xlsEOF();
exit();

Calculate Visitor Counter

Follow the step
1. Create a file name "visitcount.dat"
2. Upload the file in webserver
3. make file permission CHMOD *777*
Now Insert the PHP code below


$counterfile = "visitcount.dat";
if(!($fp = fopen($counterfile,"r"))) die ("cannot open counter file");
$count = (int) fread($fp, 20);
fclose($fp);
$count++;
echo "Total hit counter: $count";
$fp = fopen($counterfile, "w");
fwrite($fp , $count);
fclose($fp);

Tuesday, January 26, 2010

I just got my #domain @BigRock. Get upto 25% off with my personalized coupon link Get upto 25% off