Tag Archives: PHP

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

Difference between assigning a variable to NULL and unset.

<?php $var = NULL; ?>

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 unset($var); ?>

 

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.

split (":", "i:am:reading:scriptarticle.com");

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

 

 

The explode () function splits the string by string.

explode ("I", "I am reading scriptarticle.com");

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!!

Generate unique n-digit random alphanumeric codes?

Syntax

uniqid (prefix, more_entropy);

 

A unique website ID can be created in PHP using the uniqid () function.

This function has two parameters that can be set.

 

prefix

This is what will be appended to the beginning of each ID.

 

more_entropy

If this is false or not specified it will return 13 characters, if it is true then 23 characters will be returned.

 

Below are some examples:


<?php

/*creates a unique id with the 'sa' prefix*/
$a = uniqid(sa); echo $a; echo "<br />";

/*creates a longer unique id with the 'sa' prefix*/
$b = uniqid (sa, true); echo $b; echo "<br />";

/*creates a unique ID with a random number as a prefix - this is more secure than a static prefix*/
$c = uniqid (rand (),true); echo $c; echo "<br />";

/*this md5 encrypts the username from above, so its ready to be stored in your database*/
$md5c = md5($c); echo $md5c;

?>

Share your WordPress page URL on social networking website by using a simple function

Share your WordPress post on facebook & other social networking website.

 

If you want to share your WordPress page URL on social networking website, it’s very simple to do.

Just place this function in your functions.php file, and call the function where do you want it to show.

 

<?php

function GetShareLinks() {
 
/* Your current page URL */
 $URI = get_permalink();
 
/* The js script used for goolge plus share button */
 $sharestring ='<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>';
 
$sharestring .='<a href="https://pinterest.com/scriptarticle/" target="_blank"><img width="16" height="16" alt="Follow Me on Pinterest" src="http://passets-lt.pinterest.com/images/about/buttons/small-p-button.png"></a>
 <a href="http://www.facebook.com/sharer.php?u='.$URI.'"><img src="'.get_bloginfo('template_url').'/images/facebook.jpg" alt="" /></a>
 <a href="http://twitter.com/intent/tweet?text='.$URI.'"><img src="'.get_bloginfo('template_url').'/images/twitter.jpg" alt="" /></a>
 <a href="http://www.linkedin.com/shareArticle?mini=true&url='.$URI.'"><img src="'.get_bloginfo('template_url').'/images/in.jpg" alt="" /></a>
 <g:plusone count="false" size="small"></g:plusone>';
 
return $sharestring;
 
}

?>

 

You can also use this function in other website (non WordPress) site, you have to just replace the get_permalink() function by current page URL and update the social icons images path.

 

If you face any issue to use this, put your comment, I’ll help you 🙂

 

Display Recent Blog Posts Outside WordPress

Generally web master uses the WordPress for applying blog on their website.

 

Below is the code for the getting recent/latest post or list, can be applied to display recent posts outside anywhere in your WordPress website.

 

The key to use WordPress outside is include the wp-load.php file
Once the wp-load.php file is included, the entire wealth of WordPress functions will available for you to use.
This can be found under the root folder.

<?php

require_once 'wp-load.php';

// Recent 10 posts
 $recent_posts = wp_get_recent_posts(array('numberposts' => 10));

// Apply your design/html
 echo '<ul>';
 foreach($recent_posts as $post) {
 echo '<li><a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a></li>';
 }
 echo '</ul>';

?>

If you want to pass more argument, you can do, below is the WordPress reference URL for wp_get_recent_posts function
http://codex.wordpress.org/Function_Reference/wp_get_recent_posts

 

You can also do your own custom queries with WP_Query.

 

Move WordPress Blog to New Domain/Location or in another folder/directory

You usually need to move your blog (WordPress) if you are switching your web host as we generally work on localhost.You must also need to ‘move’ your website if you are transferring it to a new domain, new folder, or creating an alternate version on a local server.

After uploading file and updating wp-config.php.We have found that there are lots of URL in the database, It is very hard to update these rows one by one with new URL.
Don’t worry. It is very simple to go

Just 4 steps to do.

1. Query to Update WordPress Settings

UPDATE wp_options SET option_value = replace(option_value, ‘http://www.old-web-url.com’, ‘http://www.new-web-url.com’) WHERE option_name = ‘home’ OR option_name = ‘siteurl’;

2. Query to Update Permalinks

UPDATE wp_posts SET guid = replace (guid, ‘http://www.old-web-url.com’,’http://www.new-web-url.com’);

3. Query to Update Any Links Embedded In Content

UPDATE wp_posts SET post_content = replace (post_content, ‘http://www.old-web-url.com’, ‘http://www.new-web-url.com’);

4. Query to Post Metas

UPDATE wp_postmeta SET meta_value = replace (meta_value,’http://www.old-web-url.com’,’http://www.new-web-url.com’);

Sometimes, you have omitted the www from your domain when cross-linking posts or referencing images, so you should also run these above query without the ‘www’ for your old URL.

After following these steps, hope you will found your blog or website fully functional.
If it is still not functional, make a comment below this article with explanation what issue you are facing.