Deprecated Warning : Assigning the return value of new by reference is deprecated in * on line *

Hey there,

Did you get this Warning/Notice? :

Deprecated Warning : Assigning the return value of new by reference is deprecated in *  on line *

In PHP5 this notation is deprecated

$obj =& new Class();

That would generate the warning but ‘&’ is not required and can be removed.

$obj = new Class();

To see why this was used in PHP4, see  http://www.php.net/manual/en/oop4.newref.php

Cheers,

JENSon.




Google has blacklisted php.net website!

PHP Developer’s Home php.net has been blacklisted by Google. I think that’s probably happened ~ in between 23rd October 2013 11:15 PM IST and 24th October 2013 9:35 AM IST. Tha actual problem is that Google’s Safe Browsing technology is blocking access to PHP.net as a precaution, after apparently detecting that some of its pages were booby-trapped with links to malicious software. php.net is widley used by coders all around the globe. Google didn’t specify the types of Trojans associated with the apparent attack but it did state the domains they came from. Users visiting the PHP.net site using Google Chrome, Mozilla’s Firefox and or Apple Safari (Mozilla and Apple both license Safe Browsing from Google) were confronted by a warning firmly instructing them not to proceed any further.

PHP founder Rasmus Lerdorf on Twitter (@rasmus) tweeted :

“It appears Google has found a false positive and marked all of  http://php.net  as suspicious. pic.twitter.com/YDlHcUnCK6 “

But One Googler (http://goo.gl/8jyJRx) told in this post (http://goo.gl/Iw2Fx4) :

“What our systems found was definitely a compromised JS file, and others on this thread have posted something similar to what we saw. This is not a false positive.

We have detailed help for webmasters in this kind of situation:

http://www.google.com/webmasters/hacked/

One thing that I strongly suggest to any webmaster in this situation is to look for any server vulnerability that allowed this file to get compromised in the first place. We sometimes see webmasters simply fix the affected files without digging into security hole that allowed the hack, which leaves the server vulnerable for repeat attacks.”

Zend Co-Founder Zeev Suraski (@zeevs) replied to my tweet related to this issue:

zeev_tweet_jensons

Below are the screenshots links which shows malware warning :

Firefox Warning

Chrome Warning

Google Search for ‘php.net’

Whatever the problem my favorite website is blacklisted by Google!..:( Google will remove this warning soon (Hopefully) !

Cheers,

JENSon.

PHP : Removing Non-English Characters From String.

Hey Folks,

The below code will help you to remove non-english characters from your PHP variable string.

<?php
$string = "somAe7 სოფო_$ tഞാൻext"; 
$string = preg_replace('/[^a-z]/i', '', $string); 

echo $string; //Outputs somAetext
?>

If you want to include numbers & space from the above string, Use below code.

<?php
$string = "somAe7 სოფო_$ tഞാൻext"; 
$string = preg_replace('/[^a-z0-9_ ]/i', '', $string); 

echo $string; //Outputs somAe7 text
?>

Cheers,
JENSon.

 

 

PHP Warning: file_get_contents(): Unable to find the wrapper “https” – did you forget to enable it when you configured PHP?

Hey Folks,

Warning: file_get_contents(): Unable to find the wrapper “https” – did you forget to enable it when you configured PHP?

Got the above warning? No Problems. You can get rid of this warning by just enabling php_openssl.dll extension in your php.ini [For eg. xammp/php/php.ini] configuration file. [If you’re not seeing php_openssl.dll itself in your php.ini, just directly add the extension line to the php.ini configuration file]

Your php.ini file :

;extension=php_oci8_11g.dll
;extension=php_openssl.dll
;extension=php_pdo_firebird.dll

You just need to uncomment the line extension=php_openssl.dll.

It should look like below :

;extension=php_oci8_11g.dll
extension=php_openssl.dll
;extension=php_pdo_firebird.dll

If the extension itself does not exist, You have to add extension=php_openssl.dll to your php.ini file. (Provided you’ve the proper dll file bundled with your windows PHP environment (I’m using XAMPP & the dll extension is php_openssl.dll.)

Note : I’m using XAMPP for Windows

Cheers,
JENSon.

PHP : Deleting Duplicates From An Array

Hey Folks,

PHP’s array_unique function allows you to remove duplicate values from an array.

Syntax:

<?php
$arr = array("a" => "Ford", "Mercedes", "b" => "BMW", "Lotus", "Ford", "Renault");
$result = array_unique($arr);
echo "<pre>";
print_r($result);
echo "</pre>";

/* Output
Array
(
    [a] => Ford
    [0] => Mercedes
    [b] => BMW
    [1] => Lotus
    [3] => Renault
)
*/
?>

Note : The keys are preserved. array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.

Cheers,
JENSon.

 

 

Convert a string to an array using str_split PHP function.

Hey Folks,

In PHP, You can convert a string to an array using str_split function.

Syntax:

array str_split ( string $string [, int $split_length = 1 ] )

/* $string=The input string.
$split_length=Maximum length of the pieces. */

Example:

<?php

$str = "Awesome World";

$arr1 = str_split($str);
$arr2 = str_split($str, 3);

echo "<pre>";
print_r($arr1); 
print_r($arr2);
echo "</pre>";

/* This will output..

Array
(
    [0] => A
    [1] => w
    [2] => e
    [3] => s
    [4] => o
    [5] => m
    [6] => e
    [7] =>  
    [8] => w
    [9] => o
    [10] => r
    [11] => l
    [12] => d
)
Array
(
    [0] => Awe
    [1] => som
    [2] => e w
    [3] => orl
    [4] => d
)

*/
?>

Return Values:

If the optional split_length parameter is specified, the returned array will be broken down into pieces with each being split_length in length, otherwise each piece will be one character in length.

FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string, the entire string is returned as the first (& only) array element.

Cheers,
JENSon.

 

 

What is PDO (PHP Data Objects)? Basic Explanation.

Hey Folks,

The PDO (PHP Data Objects) is an extension which defines a lightweight, consistent interface for accessing databases in PHP. PDO provides a data-access abstraction layer, which means that, regardless of which database you’re using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn’t rewrite SQL or emulate missing features. PDO ships with PHP 5.1, and is available as a PECL extension for PHP 5.0; PDO requires the new Object Oriented features in the core of PHP 5, and so will not run with earlier versions of PHP.

Note : I assume you already have a PDO configured PHP version. If not, Please click here to see the installation process.

PDO Connections

Connecting to Database (Eg. MySQL) :

<?php $dbconn = new PDO('mysql:host=localhost;dbname=testdb', $username, $password); ?>

//Note : If there are any connection errors, a PDOException object will be thrown.

Handling Errors In Connection :

<?php
try {
    $dbconn = new PDO('mysql:host=localhost;dbname=testdb', $username, $password);
    foreach($dbconn ->query('SELECT * from tablename') as $row) {
        print_r($row);
    }
    $dbconn = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

Upon successful connection to the database, an instance of the PDO class is returned in your script. The connection remains active for the lifetime of that PDO object.

Closing a connection :


<?php
$dbconn = new PDO('mysql:host=localhost;dbname=testdb', $username, $password);
// use the connection here


// and now we're done; close it
$dbconn= null;
?>

Persistent connections :


<?php
$dbconn = new PDO('mysql:host=localhost;dbname=testdb', $username, $password, array(
    PDO::ATTR_PERSISTENT => true //If you wish to use persistent connections, you must set PDO::ATTR_PERSISTENT in the array of driver options passed to the PDO constructor.
));
?>

Multiple database connections :

try {
  $dbconn1 = new PDO('mysql:host=localhost;dbname=testdb1', $username, $password);
  $dbconn2 = new PDO('mysql:host=localhost;dbname=testdb2', $username, $password);
} catch (PDOException $ex) {
  echo 'Connection failed: ' . $ex->getMessage();
}

You can execute the query like this :

<?php
$result = $dbconn->query("select * from tablename");
foreach ($result as $row) {
  echo $row['fieldname'] . "\n";
}
?>

Fetching data using prepared statements 1 :


<?php
$query= $dbconn->prepare("SELECT * FROM tablename where fieldname = ?"); // The user input is automatically quoted, so there is no risk of a SQL injection attack. 
if ($query->execute(array($_POST['name']))) {
  while ($row = $query->fetch()) {
    print_r($row);
  }
}
?>

Fetching data using prepared statements 2 :

$query= $dbconn->prepare("select * from tablename where id = :id");
$query->execute(array(':id' => 555));
$row = $query->fetch();

Calling a stored procedure with an output parameter :


<?php
$query = $dbconn->prepare("CALL sp_returns_string(?)");
$query->bindParam(1, $return_value, PDO::PARAM_STR, 4000); 

// call the stored procedure
$query->execute();

print "procedure returned $return_value\n";
?>

I have covered only some basics here. You’ll get detailed explanation of various other functions of PDO from the php.net Website.

Cheers,
JENSon.

 

 

PHP Single Line if else Syntax

Here is the syntax for single line if else statement in PHP.

<?php
$x=15;
$y=10;
$val=($x<$y)?"Correct":"Incorrect";
echo $val;//Outputs Incorrect
?>

Cheers,
JENSon.