What is difference between assigning a variable to NULL and unset?

Difference between assigning a variable to NULL and unset.

[php]<?php $var = NULL; ?>[/php]

A variable is considered to be NULL if

It has been assigned the constant NULL.
It has not been set to any value yet.
It has been unset().

Setting a variable to NULL means assigns the value NULL to the variable exactly as the variable is set to a value and value is the special value NULL.
That means memory needs to be allocated where the NULL value is stored and an entry into the symbol table needs to be made.
The special NULL value represents a variable with no value. NULL is the only possible value of type NULL.

 

[php]<?php unset($var); ?>[/php]

 

unset() destroys the specified variables.
The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.

unset() means the variable is no longer set, it doesn’t have a value and basically the variable doesn’t exist anymore.
Unsetting a variable removes the entry for the variable from the symbol table.

 

 

$var = NULL;
var_dump($var); -> NULL

unset($var);
var_dump($var); -> NOTICE: Undefined variable var on line 21
NULL

 

 

Casting to NULL
Casting a variable to NULL using (unset) $var will not remove the variable. It only return a NULL value.

 

unset does not force immediate memory freeing but leaves it for the Garbage Collector.
$var = NULL; however forces immediate memory release.

 

So,It totally depends on the situation you have, both are good to use,but setting the value to NULL is faster.

 

PHP: What is the difference between split and explode php?

Difference between split and explode php

Do you know what is the main difference between split() and explode()?

No worries 🙂 , the main difference in split() and explode is the way, it uses to splitting a large string.

 

Both the functions are used to Split a string. However, Split is used to split a string using a regular expression. On the other hand, Explode is used to split a string using another string.

 

The split() function splits the string into an array using a regular expression and returns an array.
Ex: split (:India:Nepal:Srilanka); returns an array that contains India, Nepal, Srilanka.

 

The explode () function splits the string by string.
Ex: explode (and India and Nepal and Srilanka); returns an array that contains India, Nepal, Srilanka.

 

Have a look by below example.

[php]split (":", "i:am:reading:scriptarticle.com");[/php]

returns an array that contains i,am,reading,scriptarticle.com.

 

 

The explode () function splits the string by string.

[php]explode ("I", "I am reading scriptarticle.com");[/php]

returns an array that contains

array (
0 => ”,
1 => ‘ am reading scriptarticle.com’,
)

Note: split () function has been DEPRECATED as of PHP 5.3.0.

 

It’s been deprecated because

 

  • explode() is substantially faster because it doesn’t split based on a regular expression, so the string doesn’t have to be analyzed by the regex parser
  • preg_split() is faster and uses PCRE regular expressions for regex splits

 

join() and implode() are aliases of each other and therefore don’t have any differences.

 

Hope it helps!!

Call vPress Coreprint “getproof” Web Service using POST method

Before 3 Days, A task assigned to me. Task is related to  vPress CorePrint API  (Web2Print service).
In that what I have to do is, Client has uploaded lots of products in vPress coreprint.
The products are 3 types photo frames, wallpapers strips and one more.

 

Client have provided me a PDF for understanding the API and to let me know how to communicate with vPress API.
There are various services defined in that, some are using GET methods and some POST & some other are using PUT.

 

I have tested the GET API as the example given in the PDF and it also worked for me.
But during the service call using POST method I was stacked I have to pass some parameters using a form(in the website) and have to pass this information to the web service call using the POST method and after a successful call it return me a product image(Photo Frame).

 

Like I have to pass the information like the background colour  theme, frame name, size and it should returned me back the photo frame image(client have uploaded in the coreprint) either in the image format or in PDF.

 

I have tried a lot and have spent 2 days but not find the way it works, and have mailed many time to support but not got the proper solution.
This time Google also not supported me a lot 🙁

 

Finally someone suggested me to use cURL with PHP to post json data and It works for me.

Below are the codes that works finally 🙂 hope this will also work for you.

If the cURL returns back either a Image or PDF or we can say that if the cURL not return plain text.
Then we have to set some headers to view the result.

[php]

<?php

$key = "licence_key";
$username = "your_username";
$pass = "your_password";
$tmp = $username.":".$pass;
$productid = "your_productid";
$service = "getproof";

$postdata = $_POST;
$data_string = json_encode($postdata);
$url = "http://www.coreprint.net/ws/jsonfactory/?key=".$key."&service=".$service."&productid=".$productid;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘Content-Type: text/html’,
‘Authorization: Basic ‘.base64_encode($tmp),
‘Content-Length: ‘ . strlen($data_string))
);
$result = curl_exec($ch);

curl_close($ch);
/*Set header to display the image (returned by curl) in the browser*/
header(‘Content-type: image/jpeg’);

/*Set header to set the content type PDF (returned by curl) in the browser*/
/*
header(‘Content-Type: application/pdf’);
header(‘Content-Disposition: attachment; filename=testfile.pdf’);
readfile(‘pdfgeneratedbytheserver.pdf’);
*/

echo $result;

?>

[/php]

 

Headers for viewing the image returned by cURL.

[php]
/*Set header to display the image (returned by curl) in the browser*/
header(‘Content-type: image/jpeg’);
[/php]

Headers for viewing the PDF returned by cURL.

[php]
/*Set header to set the content type PDF (returned by curl) in the browser*/
header(‘Content-Type: application/pdf’);
header(‘Content-Disposition: attachment; filename=testfile.pdf’);
readfile(‘pdfgeneratedbytheserver.pdf’);
[/php]

Hope this all will help you, let me know by your comment if you still face any issue in implementing vPress coreprint API with post method.

 

Hotlinking/bandwidth theft, check hotlinking and preventing hotlinking

Hot linking can also be called as “leeching” or “bandwidth theft” is a term referring to when a webpage of one website is direct linking to the images/videos or other multimedia files on the web host of another website (usually without permission, thus can be called as stealing bandwidth).

 

E.g. Like an <img  alt=”” /> tag to display an image you found on someone else webpage so it will appear on your site, eBay auction listing, weblog, forum message post, etc.

 

Bandwidth can be referred as the amount of data transferred from a website to a user’s computer. Whenever you view a web page, you use that site’s bandwidth to display the files. Since web hosts charge based on the amount of data transferred, bandwidth is the issue. If a site is over its monthly bandwidth, it’s billed for the extra data or taken offline.

 

One of the most common occurrences of “hot linking” is when people post on a forum and they hotlink pictures from another website to use as avatars or signature images on the message boards.

 

Some disadvantages of hot linking are that the webpage generally loads slower when you link to images stored on a different server than the webpage is hosted on, and the owner of the image has full control to disable hot linking, or delete, rename, or make worst it, do a “switcheroo” (i.e. switching the file name to be another image which is sure to cause the hot linker embarrassment) of the hot-linked image.

 

Common methods of preventing hot linking are by using an .htaccess file, using the “Hotlink Protection” offered in control panels such as Cpanel, or simply renaming image files periodically.

 

HOW COULD YOU KNOW THAT YOU ARE HOTLINKING?

<img src=”image.jpg” height=”350″ width=”200″>
<img src=”http://notyourwebsite.com/image.jpg” height=”350″ width=”200″>

This img tag tells the site to request the image.jpg from a different server. Every time the page is loaded, the outside server has to use its bandwidth to display the image. To avoid this problem, don’t link to files on servers. To share images and files on your own web page, upload them to your own server’s directory or to a free image hosting service that allows direct linking.

Below are some free image/video hosting service providers

http://www.dropshots.com/
http://photobucket.com/

 

DRAWBACK OF HOTLINKING

Hot linking can have a lot of undesirable results. One is the so-called “switcheroo“.
Displaying an image or file that doesn’t belong to could be a violation of copyright, making you open to litigation. The owner of the file could utilize DMCA law to have your site shut down and your information given for use in legal proceedings.

 

HOW CAN YOU TEST YOUR WEBSITE HOTLINKED?

Enter the complete URL (ex: http://yoursitename.com/image.jpg) to see if your image can be loaded and hot linked by other server.
http://www.free-webhosts.com/hotlinking-checker.php

 

PREVENT HOTLINKING USING .htaccess

Below code can be written in .htaccess file to stop hot linking and bandwidth theft.

[sourcecode language=”plain”]
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ http://www.yourdomain.com/badimage.jpg [R,L]
[/sourcecode]

Replace yourdomain.com to your domain name and badimage.jpg to your image which you want to show instead of hot linked image.

Please be sure your mod_rewrite is ON on your server before above .htaccess updates.

 

Interesting and amazing bugs in WINDOWS OS

Bugs are the truth of every developer.
Even the windows developed by IT Giant Microsoft have also some bugs in it.
May be you have noticed or not. Check it out it’s interesting and magical 🙂

 

MAGIC #1

 

Nobody can create a FOLDER anywhere on the Computer which can be named as “CON”. This is something funny and inexplicable? At Microsoft the whole Team, couldn’t answer why this happened! TRY IT NOW, IT WILL NOT CREATE A “CON” FOLDER.
It is not just for “CON”.This is also followed by PRN, AUX, CLOCK$, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9

 

MAGIC #2

 

For those of you using Windows (only Windows XP), do the following:
1.) Open an empty notepad file
2.) Type “Bush hid the facts” (without the quotes)
3.) Save it with a name as whatever you want.
4.) Close it, and re-open it.
Noticed the weird bug?  The original text has been converted to some garbage text!

 

MAGIC #3

 

Again this is something funny and can’t be explained?
At Microsoft the whole Team, including Bill Gates, couldn’t answer why this happened!
It was discovered by a Brazilian. Try it out yourself?
Open Microsoft Word and type
=rand (200, 99)
And then press ENTER and see the magic?

ha ha ha how’s it?

 

Magic #4

 

Did you know that a flight number from one of the planes that hit one of the two WTC towers on 9/11 was Q33N. In Notepad / WordPad or MS Word, type that flight number i.e Q33N. Increase the font size to 72. Change the font to Wingdings. ….. You will be amazed by the findings!!

 

Guys, don’t worry about the bugs. You can’t run beyond the bugs. So carry on your good coding and programming and fix the bugs at your level best.

 

Test and debug your code online

During program or code development we face lot of issues and bugs and in between we need some debugging.
In PHP there is not a easy way to debug the page line by line, so you have to use ‘echo’,’die’,’print_r’,’var_dump’ like functions for debugging.

 

It take a little bit more time to open the page add these function and then view that page again by refreshing the browser.

 

Here on internet you have lot of websites the providing the same functionality to check the variable in very less time and without reviewing code again and again.

 

I am listing out some of these as follows:

1. json_encode and json_decode parser online tool
http://json.parser.online.fr/

2. base64_decode and base64_encode

3. Array serialize and unserialize

4. Format date and date related function

5. md5,preg_replace ,preg_match

6. String related functions
http://www.tools4noobs.com/online_php_functions/

 

7. Count characters in a string and words in a sentence
http://allworldphone.com/count-words-characters.htm

 

8. JS beautifier (to format your JavaScript file)
http://jsbeautifier.org/

 

9. Format your HTML page (format your HTML page)
http://www.freeformatter.com/html-formatter.html

 

10. W3C validate
http://validator.w3.org/#validate_by_input

 

RUN/Execute your small php code online with any version of PHP.
http://sandbox.onlinephpfunctions.com/

 

RUN/Execute your JavaScript code to view the result online.
http://writecodeonline.com/javascript/

 

RUN/Execute jQuery code online to test
http://jsfiddle.net/

 

List of All PHP Functions
http://php.net/manual/en/indexes.functions.php
http://php.net/quickref.php

 

Okay guys, Try out these hope these will help you in debugging the code.
All the best!!