Crushing Your Next PHP Interview: Essential Questions and Answers (2024 Edition)
1. Is multiple inheritance supported in PHP?
Answer : PHP support only single inheritance; it means that a class can be extended from only single class using the keyword 'extended'.
<?php class Production { function brand() { echo "Player Of Code"; } } class Development extends Production { function work() { echo "Web Development"; } } $obj= new Development(); $obj->brand(); //Outout => Player Of Code ?>
2. What is difference between require() and require_once()?
Answer : require() and require_once() do the same task but require_once() checks if the PHP script is already included or not before executing it.
same for include() and include_once()
3. How can we connect MySQL database with PHP?
Answer: To connect mysql database with php. We use mysqli_connect() which takes four parameters : host name, username, password and database.
<?php $hostname="localhost"; $username="root"; $password=""; $database="playerofcode"; $con=mysqli_connect($hostname, $username, $password, $database); if($con): echo "PHP script connected with mysql database"; else: echo "Unable to connect"; endif; ?>
4. How to check the value of a given variable is alphanumeric?
Answer: ctype_alnum() is used to check given variable is alphanumeric or not.
<?php $value="Playerofcode123"; $res=ctype_alnum($value); if($res): echo "Alphanumeric"; else: echo "Not a alpha numeric"; endif; //output : Alphanumeric ?>
5. How to delete file in php?
Answer: the unlink() is used to delete the file in php.
<?php $file="slider.jpg"; $res=unlink($file); if($res): echo "File deleted successfully"; else: echo "Something went wrong"; endif; //output : File deleted successfully ?>
6. Difference between Having and Group by.
The HAVING clause is used instead of WHERE with aggregate functions. While the GROUP BY Clause groups rows that have the same values into summary rows.