Learn PHP Basics – For Freshers and Experienced
Database Connection
Connect mysqli with php using Object-Oriented Way.
$host = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($host, $username, $password);
//connect to server
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
Connect mysqli with php using Procedural Way.
$host = "localhost";
$username = "root";
$password = "";
$dbname = "cmscomputer";
// Create connection
$conn = mysqli_connect($host, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Connect mysqli with php using PDO.
$host = "localhost";
$username = "root";
$password = "";
$conn = new PDO("mysql:host=$host;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
convert string to uppercase in php
$string="cms computer training center";
echo strtoupper($string);
convert string to lowercase in php
$string="CMS COMPUTER TRAINING CENTER";
echo strtolower($string);
convert first letter of string to uppercase in php
$string="cakephp and zend";
echo ucwords($string);
delete a php file from server
$file="full_path/filename.php"
unlink($file); //make sure you have enough permission to do delete the file.
convert array to json in php
$array = array('one','two');
echo json_encode($array); //use json_decode for decode
serialize an array in php
$array = array('php','mysql');
echo serialize($array);//use unserialize for convert serialized string to array
get ip address in php
$_SERVER['HTTP_USER_AGENT']
count the number of elements in an array
$array = array('html','css');
sizeof($array);
count($array);
SESSION and SESSION Properties and Functions.
A session is a logical object enabling us to preserve temporary data across multiple PHP pages.
1. Use function session_start()
to initiate a session.
2. It is possible to propagate a session id via cookies or URL parameters.
3. Sessions automatically ends when the PHP script finishes executing, but can be manually ended using the session_write_close()
.
4. The session_unregister()
function unregister a global variable from the current session and the session_unset()
function free all session variables.
5. To destroy all data registered in a session use function session_destroy()
;