Php-mysql Course

In this tutorial there are presented Some common PHP errors, posible cause and solution to fix them.
List of PHP errors presented in this page:
unexpected '/', unexpected '{', unexpected $end, Could not connect to SMTP host, mysql_num_rows() expects parameter 1 to be resource, unexpected T_STRING, unexpected T_ECHO, session had already been started, unexpected T_VARIABLE expecting '.' or ';', Use of undefined constant, Call to undefined function, headers already sent, Undefined variable, Undefined index /Undefined offset, unexpected T_IF, unexpected T_FOR, unexpected ')', Illegal offset type, unexpected '=' expecting ')', unexpected ... T_CONSTANT_ENCAPSED_STRING expecting ..., unexpected T_BOOLEAN_AND, unexpected '$variable' (T_VARIABLE) expecting function (T_FUNCTION), unexpected 'some_string' (T_STRING) expecting function (T_FUNCTION), unexpected '$var_name' (T_VARIABLE), unexpected '->' (T_OBJECT_OPERATOR), Missing argument ... for function_name(), zip_read() expects parameter 1 to be resource, syntax error unexpected 'unset' (T_UNSET) in ..., preg_match(): Compilation failed: nothing to repeat at offset, Cannot redeclare class, Call to undefined function curl_init(), Expects parameter 1 to be string; array given in, Expects parameter 1 to be array; string given in, domdocument.loadhtml Unexpected end tag, simplexml_load_string() / loadXML() Input is not proper UTF-8 indicate encoding, session_start() The session id is too long or contains illegal characters, preg_replace / preg_match Unknown modifier, Invalid argument supplied for foreach(), Call to undefined method stdClass, Call to a member function bind_param() on boolean, preg_replace Empty regular expression, Class finfo not found
- First, to be sure that PHP displays all errors and warning messages, add this code at the beginning of the PHP script.
ini_set('display_errors',1);
error_reporting(E_ALL);

Parse error: syntax error, unexpected '/' in ...
Cause:
- Posible forgot a slash "/" in the comment line.
<?php
/ Wrong comment line in PHP
Solution:
- Add the second slash at the beginning of the comment line.
<?php
// Correct comment line
Parse error: syntax error, unexpected '{' in ...
Cause:
- Posible to miss a closing ')' to a conditional instruction.
<?php
if(isset($variable) {
 // missing the closing ')' for if()
}
Solution:
- Add the closing ')'.
<?php
if(isset($variable)) {
 // php instructions
}
Parse error: syntax error, unexpected $end in
Cause:
- The cause of this error can be the editor used to edit the PHP file.
The complex editors, like DreamWaver, or the editor from CPanel can add invisible characters on the first line of the file (a UTF-8 byte order mark at the top of file), which causes the error.
This can also be caused if a website is coded in ASCII and php files are being saved as UTF-8.
Solution:
- Try use a simple text editor, for example Notepad++, and set Encoding UTF-8 without BOM.
If the website and database are both UTF-8, it should be ok to save php files as UTF-8.
SMTP Error: Could not connect to SMTP host
Cause:
- PHP cannot connect to the SMTP mail server. A cause can be incorrect name or passord for connecting to SMTP server.
- If you use PHPMailer class, the cause of this error can be: The extension=php_openssl.dll inactivated in "php.ini", or, the file php_openssl.dll is missing from PHP.
Solution:
1. Add correct data for connecting to SMTP server.
2. Add this line in "php.ini":
extension=php_openssl.dll
- Or delete the " ; " character if this line exists, but with ; in front.
3. Use a PHP version that contains the "php_openssl.dll" file.
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in ...
Cause:
The cause can be incorrect SQL query that makes MySQL server to return False.
Solution:
- Check the SQL query you send to MySQL (you can test it in PHPMyAdmin), this can help you to see and fix the problem in that query.
<?php
// connect to MySQL
$sql = "SELECT * FROM `table_name` WHERE $condition ...";
echo $sql; // outputs in browser the sql query that is send to MySQL
Parse error: syntax error, unexpected T_STRING in ...
Cause:
- Possible to miss the proper single, or double quote for starting /closing string. The cause for this error can be the incorrect spelling of the PHP instructions.
<?php
// missing single quote to close the string
$variable = 'Web Programming Courses https://coursesweb.net";
echo $variable; // same error iff($variable == 8) echo 8; // wrong "iff" instead of "if"
Solution:
- Add the proper simple, or double quote to delimiter the string. Check and correct the spelling of PHP instructions at the line indicated in the error message.
<?php
$variable = 'Web Programming Courses https://coursesweb.net" ';
echo $variable; if($variable == 8) echo 8;
Parse error: syntax error, unexpected T_ECHO in
Cause:
- Missing the character ";" that ends a code instruction.
<?php
// missing the ; at the end of code line
$variable = 'Courses, Games, Anime: marplo.net'
echo $variable;
Solution:
- Check the code and add the ";" character at the end of code line where it is missing.
<?php
$variable = 'Courses, Games, Anime: marplo.net';
echo $variable;
Notice: A session had already been started - ignoring session_start() in
Cause:
- This error appear when the session_start() it is used two times in same script. This thing can happen when you add session_start() in the code, and, in that php file it is included another php file which contains session_start().
<?php
session_start();

// include a file which contains session_start()
include('file.php');
Solution:
- Add allways session_start() with a condition that checks if $_SESSION exists.
<?php
if(!isset($_SESSION)) session_start();
Parse error: syntax error, unexpected T_VARIABLE, expecting '.' or ';' in ... on line ...
Cause:
- You can get this error if it is missing a ';' character (to finish a line of code, or in the code of a loop instruction). Or it is missing a concatenation operator '.' in a string.
<?php
// missing the ';' after 8
for($i=0; $i<8 $i++) {
 // some php code
}

// same error, the ';' is not added at the end of code-line
$v = 'some value'

// same error, it is missing the concatenation character '.'
echo 'V is: ' $v;
Solution:
- Check your code at the indicated line (or a little above), and add the missing ';', or concatenation '.' .
<?php
for($i=0; $i<8; $i++) {
 // some php code
}
$v = 'some value';
echo 'V is: '. $v;
Notice: Use of undefined constant name - assumed 'name' in ... on line ...
Cause:
- It is accessed a "name". constant that is not defined in the script till that line. Or it is used a variable without the $ character.
<?php
$num = 789;
echo num; // missing the $
Solution:
- If you use a constant, define that constant before access it. Or, if it is a variable, add the $ character to that variable name.
<?php
$num = 789;
echo $num;
Fatal error: Call to undefined function function_name() in ... on line ...
Cause:
- It is accessed a function that is not defined in the script till that line.
<?php
$num = function_name();
Solution:
- Create that function before access it.
<?php
// create the function
function function_name() {
 // code of the function
}
$num = function_name();
Warning: Cannot modify header information - headers already sent by ...
Cause:
- The general cause is that the PHP script sends output data to browser, then it is ussed: session_start(), setcookie(), or header().
- If this error is not the first error message on the page, then it is most likely a 'avalanche effect' of previous error that was send to browser.
- This can also be caused if a website is coded in ASCII and php files are being saved as UTF-8.
- The problem can also be from an extra whitespace (or other text) before the beginning "<?php", or after the ending "?>" in the PHP files included in script.
Whitespace, or some HTML code<?php
session_start();
header('Content-type: text/html; charset=utf-8');
Solution:
- Use a simple text editor, for example Notepad++. If the website and database are both UTF-8, it should be ok to save php files as UTF-8 without BOM.
- Read the error message carefully. It says "output started at ..." followed by a file name and a line number. That is the file (and line) that you need to edit.
- Add session_start() at the beginning of the php file.
- Make sure you send all your headers before you start sending the content.
- Remove extra whitespace before the beginning "<?php", or after the ending "?>" in the PHP files included in script.
- When use header() function, check if Header already send.
<?php
session_start();
if(!headers_sent()) header('Content-type: text/html; charset=utf-8');
Notice: Undefined variable: var_name in ...
Cause:
- It is accessed a variable that is not defined in the script till that line.
<?php
// access an undefined variable
echo $var_name;
Solution:
- Create that variable before access it. Also, a good practice is to use isset() to check if that variable exists.
<?php
$var_name = 'value';
if(isset($var_name)) echo $var_name;
Notice: Undefined index /Undefined offset: xy in ...
Cause:
- It is accessed an array index /key (array element) that not exists in that array, you're referring to an array key that doesn't exist.
"Offset" refers to the integer key of a numeric array, and "index" refers to the string key of an associative array..
<?php
$aray = array('ab'=>'val 1', 2=>78);
echo $aray['xy']; // the key /index "xy" not exists in $aray
Solution:
- Try to create the element with that index /key in array, to access only defined array elements. Also, use isset() to check if that element exists, useful when the array is defined and accesed dinamically.
<?php
$aray = array('ab'=>'val 1', 2=>78);
if(isset($aray['xy'])) echo $aray['xy'];
Parse error: syntax error, unexpected T_IF in ... on line ...
Cause:
- A syntax error before a "if()" condition. Usually, this error is caused by the missing of the ';' character before a line with a "if()" condition.
<?php
$v = 8 // missing ;
if($v == 8) echo $v;
Solution:
- Check the code immediately before the line indicated in the error message, and add the correct syntax.
<?php
$v = 8;
if($v == 8) echo $v;
Parse error: syntax error, unexpected T_FOR in ... on line ...
Cause:
- A syntax error before a "for()" instruction. Usually, this error is caused by the missing of the ';' character before a line with a "for()" instruction.
<?php
$v = 2 // missing ;
for($i=0; $i<3; $i++) echo $v + $i;
Solution:
- Check the code immediately before the line indicated in the error message, and add the correct syntax.
<?php
$v = 2;
for($i=0; $i<3; $i++) echo $v + $i;
Parse error: syntax error, unexpected ')' in ... on line ...
Cause:
- An extra ")".
<?php
$v = 2;
if($v == 2)) echo $v; // there is an extra ')'
// Or:
file_get_contents('page.html')); // two ')'
Solution:
- Check the code at the line indicated in the error message, and delete the extra ')'.
<?php
$v = 2;
if($v == 2) echo $v;
// Or:
file_get_contents('page.html');
Warning: Illegal offset type in ... on line ...
Cause:
- This warning message can appear when you use a Function, an Array, an Object, or other incorrect value as the index /key of an Array element.
<?php
$id = function(){ return 8; };
$aray[$id] = 'https://coursesweb.net/'; // $id contains a function
// OR:
$id = array();
$aray[$id] = 'https://marplo.net/'; // $id is an Array
Solution:
- Check the code at the line indicated in the error message, and use a variable that contains a string, or numeric value for the index /key of the array element.
<?php
function fName(){ return 8; }
// $id contains the result returned by the function
$id = fName();
$aray[$id] = 'https://coursesweb.net/';
// OR:
$id = 'id';
$aray[$id] = 'https://marplo.net/';
Parse error: syntax error, unexpected '=', expecting ')' in ... on line ...
Cause:
- This error can appear when it is missing the '>' character between the key and its value in array.
<?php
$aray = array(0=>'abc', 1='xyz', 2=>789);         // missing '>' at key 1
Solution:
- Check the code (the array) at the line indicated in the error message, and add the missing '>'.
<?php
$aray = array(0=>'abc', 1=>'xyz', 2=>789);
Parse error: syntax error, unexpected ... T_CONSTANT_ENCAPSED_STRING, expecting ...
Cause:
- This error can appear when it is missing the '=>' between the key and its value in array, or the comma separator between array items, or when it is missed the dot character after a variable concatenated /joined to string.
<?php
$aray = array(0=>'abc', 1'xyz', 2=>789); // missing '=>' after key 1

// Or, missing comma ',' between array items
$arr = array('url'=> 'https://coursesweb.net/' 'pr'=> 5);

// Or, missing dot '.' after variable concatenated /joined to a string
$var = 'some val';
echo 'Text '. $var '<br/>';
Solution:
- Check the code (the array, or concatenated string) at the line indicated in the error message, and add the missing character.
<?php
$aray = array(0=>'abc', 1=>'xyz', 2=>789);

$arr = array('url'=> 'https://coursesweb.net/', 'pr'=> 5);

$var = 'some val';
echo 'Text '. $var .'<br/>';
Parse error: syntax error, unexpected T_BOOLEAN_AND in ... on line ...
Cause:
- This error can appear when you add a function as the argument of a PHP function.
<?php
// get extension (the part after the last '.')
$filename = ' https://coursesweb.net.html';
$extension = end( explode('.', $filename) );         // explode() as argument in end()
echo $extension;
Solution:
- Store into a variable the result of the function, then use the variable for argument of the other function.
In other words: Pass a real variable for argument and not a function returning an array because only actual variables may be passed by reference.
<?php
// get extension (the part after the last '.')
$filename = ' https://coursesweb.net.html';
$arr_exp = explode('.', $filename);         // stores the result of explode() into a variable
$extension = end($arr_exp);         // uses the variable
echo $extension;
Parse error: syntax error, unexpected '$variable' (T_VARIABLE), expecting function (T_FUNCTION) in ... on line ...
Cause:
- This error can appear when it is missing the access attribute (public, private, protected) from a property into a class.
<?php
class test {
 $property = 'coursesweb.net'; // missing the required attribute (public, private, protected)

 public function method(){
 return $this->property;
 }
}
Solution:
- Check the code in the file, at the line indicated in the error message, and add the access attribute correctly, before the variable name.
<?php
class test {
 public $property = 'coursesweb.net';

 public function method(){
 return $this->property;
 }
}
Parse error: syntax error, unexpected 'some_string' (T_STRING), expecting function (T_FUNCTION) in ... on line ...
Cause:
- This error can appear when the access attribute of a property into a class is spelled incorrectly, or it is added other thing than one of these words: public, private, protected.
<?php
class test {
 prvate $property = 'marplo.net'; // the access attribute is spelled incorrectly (should be: private)

 public function method(){
 return $this->property;
 }
}
Solution:
- Check the code in the file, at the line indicated in the error message, and write the access attribute correctly (public, private, protected), before the property name.
<?php
class test {
 private $property = 'marplo.net';

 public function method(){
 return $this->property;
 }
}
Parse error: syntax error, unexpected '$var_name' (T_VARIABLE) in ... on line ...
Cause:
- This error can appear when the property into a class is set using directly other variable to devine its value.
<?php
$v = 'value';
class test {
 private $prop1 = $v; // incorrect (should be directly a sting, number, or array)
 public $prop2 = $_GET; // incorrect too
 // ...
}
Solution:
- Check the code in the file, at the line indicated in the error message, and define the property value directly with a sting, number, or array. Or create it with no value, then you can set the property value with other variable in _constructor().
<?php
class test {
 private $prop1 = 'value';
 public $prop2;

 function __constructor() {
 $this->prop2 = $_GET;
 }
}
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in ... on line ...
Cause:
- This error can appear when it is missing the '$' character before the special word "this", or before the object name, when it is accessed a property or method.
<?php
class test {
 private $property = ' https://coursesweb.net/php-mysql/';

 public function method(){
 return this->property; // missing the '$' before 'this'
 }
}

// Same mistake
$obj = new test();
echo obj->method(); // missing the '$' before 'obj'
Solution:
- Check the code in the file, at the line indicated in the error message, and add the missing '$'.
<?php
class test {
 private $property = ' https://coursesweb.net/php-mysql/';

 public function method(){
 return $this->property;
 }
}

$obj = new test();
echo $obj->method();
Warning: Missing argument ... for function_name(), called in ... on line ...
Cause:
- This error can appear when it is called a function without passing all the rquired arguments. Then, might be fallowed by this error:
    • Notice: Undefined variable: variable_name in ... , because of the missing argument.
<?php
// function with 3 parameters
function test($a, $b, $c) {
 return ($a * $b) / $c;
}

// calls the function, passing only 2 arguments
echo test(8, 9);
Solution:
- Check the code in the file, at the line indicated in the error message, and add the required arguments in the call of the function.
<?php
// function with 3 parameters
function test($a, $b, $c) {
 return ($a * $b) / $c;
}

// calls the function, passing all required arguments
echo test(8, 9, 10);
Warning: zip_read() expects parameter 1 to be resource, integer given in ... on line ...
Cause:
- It's because zip_open() failed to open the file and returned an error code instead of a resource. This error might be fallowed by this:
    • Warning: zip_close() expects parameter 1 to be resource, integer given in ... , because of the same cause.
<?php
$zip_file = 'file.zip';
$zip = zip_open($zip_file);

if($zip) {
 while ($zip_entry = zip_read($zip)) {
 // ...
 }
 zip_close($zip);
}
else echo "Failed to open $zip_file";
Solution:
- Check if the path and name of the ZIP file are correct. If they are correct, try to use the FULL path to the ZIP file.
<?php
$zip_file = getcwd(). '/file.zip'; // Full path of the "file.zip"
$zip = zip_open($zip_file);

if($zip) {
 while ($zip_entry = zip_read($zip)) {
 // ...
 }
 zip_close($zip);
}
else echo "Failed to open $zip_file";
syntax error, unexpected 'unset' (T_UNSET) in ...
Cause:
- This error can appear when the unset() function is assigned to a variable, or it is used with another PHP function (for example with if() ).
<?php
$arr = array('https://coursesweb.net/', 'https://marplo.net/');
$varname = unset($arr[0]); // error

// incorrect
if(unset($arr[0])) {
 echo 'unset';
}
Solution:
- unset() returns No Value, so, it cannot be assigned, or compared. It removes an element from Array, just apply it as it is.
<?php
$arr = array('https://coursesweb.net/', 'https://marplo.net/');
unset($arr[0]);
preg_match(): Compilation failed: nothing to repeat at offset ...
Cause:
- This warning message appears when preg_match() is used with an incorrect RegExp pattern. So, the problem is in the RegExp pattern, usualy if the "*" is added wrong.
Example:
<?php
$str = '<a href="https://coursesweb.net" title="Web Programming Courses">CoursesWeb.net</a>';

// the "*" added wrong. Should be (.*?)
if(preg_match('/href="(.?*)"/i', $str, $mt)) print_r($mt[1]);

// {8,28} and * cannot be used together, you can only use one or the other
if(preg_match('/href="([^"]{8,28}*)/i', $str, $mt)) print_r($mt[1]);
Solution:
- Check the RegExp pattern at the character number specified in the Warning message (after 'offset ...'), and correct the syntax.
<?php
$str = '<a href="https://coursesweb.net" title="Web Programming Courses">CoursesWeb.net</a>';

if(preg_match('/href="(.*?)"/i', $str, $mt)) print_r($mt[1]);

if(preg_match('/href="([^"]{8,28})/i', $str, $mt)) print_r($mt[1]);
Cannot redeclare class ...
Cause:
- There is a class of the same name declared more than once. Maybe via multiple includes.
Example:
<?php
include('file_with_class_Foo.php');

// include other php file which includes in its code the 'file_with_class_Foo.php'
// or, it is created a class of same name

class Foo {}
Solution:
- Instead of using include() (or require() ) use include_once() (or require_once() ) in all the php files in which that class is included, to prevent multiple inclusions.
<?php
include_once('file_with_class_Foo.php');

// include other php file which includes in its code the 'file_with_class_Foo.php', but using include_once()
Call to undefined function curl_init()
Cause:
- The CURL extension is not installed or enabled in your PHP.
Solution:
- If PHP has installed the "php_curl.dll", you have to enable curl. Go to your php.ini file and remove the ; mark from the beginning of the following line:
;extension=php_curl.dll
Expects parameter 1 to be string; array given in
Cause:
- This warning message appears when a function that receives string for parameter it is called with an array argument.
For example, it returns this error when you call the trim() or strip_tags() function (or other function for strings), and the argument is an array.
Also, you can get this error when the string function is called as callback via another function, for example via array_map(), and the passed argument is an multidimensional array.
<?php
$val = array(8, 9);
$val = trim($val); // returns error
$val2 = strip_tags($val); // returns error

 /* Example with array_map() */

$arr = array(8, 'id'=>array(8, 9)); // multidimensional array
$arr = array_map("trim", $arr); // returns error
Solution:
- Check if the value for function argument is a string.
- If the function is called via an array_map(), you can check that the second parameter to be a simple array (not multidimensional array), with this condition:
count($array) == count($array, COUNT_RECURSIVE)
Example:
<?php
$val = 'some string';

// if $val is string
if(is_string($val)) {
 $val = trim($val);
 $val2 = strip_tags($val);
}
else {
 // $val not string
}

 /* Example with array_map() */

$arr = array(8, 'id'=>array(8, 9)); // multidimensional array

// if $arr is an array, not multidimensional array
if(is_array($arr) && count($arr) == count($arr, COUNT_RECURSIVE)) {
 $arr = array_map("trim", $arr);
}
else {
 echo '$arr not simple array';
}
Expects parameter 1 to be array; string given in
Cause:
- This error appears when a function for array (array_unique(), key(), array_sum(), ...) is called with a string value for parameter.
<?php
$val = 'some string';
array_unique($val); // returns error
Solution:
- Check if the value for argument is an array, with "is_array()".
<?php
$val = 'some string';
if(is_array($val)) array_unique($val);
else echo '$val not array';
domdocument.loadhtml Unexpected end tag
Cause:
- This error appears when you are passing an invalid HTML to the loadHTML() method; for example unclosed tag or attribute. Invalid HTML is quite common, DOMDocument::loadHTML corrects most of the problems, but gives warnings by default.
<?php
// string with invalid html content
$html = '<div><a href="https://coursesweb.net/" title="Courses Web">CoursesWeb.net</a></span>';
$doc = new DOMDocument();
$doc->loadHTML($html); // loadHTML(): Unexpected end tag : span in
Solution:
- With "libxml_user_internal_errors()" you can control that behavior. Set it before loading the document:
<?php
// string with invalid html content
$html = '<div><a href="https://coursesweb.net/" title="Courses Web">CoursesWeb.net</a></span>';
$doc = new DOMDocument();
libxml_use_internal_errors(true);   // the trick
$doc->loadHTML($html);
simplexml_load_string() / loadXML() Input is not proper UTF-8 indicate encoding
Cause:
- The error occurs when the xml has some invalid characters that do not fit in the utf-8 character set.
- The solution is quite simple. Just convert the entire xml string to ut8 (with utf8_encode() function) and then load. The utf8_encode() function will convert the string to proper utf8 and invalid characters would be fixed, making the xml parseable by simplexml or domdocument.
<?php
$xml = simplexml_load_string( utf8_encode($string_xml) );
session_start() The session id is too long or contains illegal characters
Cause:
- session_start() generate a warning if PHPSESSID contains illegal characters.
- To avoid, use this code:
<?php
// start session (if isn't started)
if(!isset($_SESSION)) {
 $sn = session_name();
 if(isset($_COOKIE[$sn])) $sessid = $_COOKIE[$sn];
 else if(isset($_GET[$sn])) $sessid = $_GET[$sn];

 if(!isset($sessid) || preg_match('/^[a-z0-9,\-]{22,40}$/i', $sessid)) session_start();
 else {
 session_id(uniqid());
 session_start();
 session_regenerate_id();
 }
}
preg_replace / preg_match Unknown modifier
Cause:
- This error appears when you not add delimiters for RegExp pattern in functions like preg_replace() or preg_match(). Or, you not escape the delimiter character used in the RegExp pattern.
<?php
$str = 'Test php regexp, 12 az https://coursesweb.net/ code preg_replace()';

// missing delimiters
$str = preg_replace('[A-z0-9]{0,2}i', '', $str); // returns error

// using delimiters, but the delimiter "/" inside RegExp not escaped
if(preg_match('/(.+?)/coursesweb/i', $str, $mt)) {
 echo 'ok';
}
Solution:
- Use delimiters with regexes in PHP. You can use the often used /, but PHP lets you use any matching characters, so @ and # are popular.
- If the delimiter character is used in the RegExp pattern, escape it with backslash "\".
<?php
$str = 'Test php regexp, 12 az https://coursesweb.net/ code preg_replace()';

// # delimiters
$str = preg_replace('#[A-z0-9]{0,2}#i', '', $str);

// slash delimiters "/", used also inside RegExp, and escaped
if(preg_match('/(.+?)\/coursesweb/i', $str, $mt)) {
 echo 'ok';
}
Invalid argument supplied for foreach()
Cause:
- This error appears when you use foreach() to traverse a variable which is not an Array.
<?php
$arr = 'not an array';
foreach($arr as $k => $v) { // returns error because $arr isn't an array
 // do something with $k /$v
}
Solution:
- Use is_array() function to check if is an Array the variable added in foreach().
<?php
$arr = 'not an array';
if(is_array($arr)) {
 foreach($arr as $k => $v) {
 // do something with $k /$v
 }
}
Call to undefined method stdClass
Cause:
- This error appears when you add dinamically a method to a stdClass object and try to execute it directly.
<?php
$ob = new stdClass();
$ob->method = function($arg){ echo $arg;};
$ob->method('val'); // generates Fatal error
Solution:
- Assign the stdClass object method to a variable, then access that variable like a function.
<?php
$ob = new stdClass();
$ob->method = function($arg){ echo $arg;};
$func = $ob->method; // assign method to a variable
$func('val'); // it works
Call to a member function bind_param() on boolean
Cause:
- This error is likely because there is an issue with the SQL query. The prepare() might return FALSE (a Boolean).
Solution:
- Check the prepare() statement for truthiness and if it is good you can proceed on to binding and executing, otherwise, get the SQL error which you can use to solve the issue.
$sql = "INSERT INTO table_name (col_1, col_2, col_3) VALUES (?,?,?)";
if($query = $mysqli->prepare($sql)) { // assuming $mysqli is the connection
 $query->bind_param('sss', $val1, $val2, $val3);
 $query->execute();
 // any additional code you need would go here.
} else {
 echo $mysqli->errno .' - '. $mysqli->error; //Show SQL error message
}
preg_replace Empty regular expression
Cause:
- Usually, this warning message appears when the regular expression in preg_replace() function is emtpy; the regex is not surrounded by delimiters.
$str ='good strings 12';
$str2 = preg_replace(' ', '-', $str); // Warning: preg_replace(): Empty regular expression
echo $str2;
Solution:
- Add a start/stop delimiter to the regular expression (for example, #).
$str ='good strings 12';
$str2 = preg_replace('#\s+#', '-', $str);
echo $str2; // good-strings-12
Class finfo not found
Cause:
- This error appears if the extension finfo it is missing, or it is not enabled in PHP.
Solution:
- PHP 5.3.0 and later have Fileinfo built in, but on Windows you must enable it manually in your php.ini. Have a look at your php.ini file and check that the fileinfo.so , or php_fileinfo.dll is activated (depending on your platform and version).

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
Common PHP Errors and Solutions

Last accessed pages

  1. Select in two MySQL tables (5326)
  2. PHP MySQL - WHERE and LIKE (29273)
  3. Vue JS - Short presentation (380)
  4. Node.js Move and Copy file (28190)
  5. PuzzleImg - Script to Create Image Puzzle Game (9371)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (322)
  2. PHP Unzipper - Extract Zip, Rar Archives (112)
  3. Read Excel file data in PHP - PhpExcelReader (103)
  4. SHA1 Encrypt data in JavaScript (82)
  5. Get and Modify content of an Iframe (75)
Chat
Chat or leave a message for the other users
Full screenInchide