![php with javascript, Jquery and ajax](../content_image/blog/ecb0f919-0542-4018-8fed-893404cd27c0.webp)
18
Oct 2024PHP with MySQL, Javascript and more
Description
What is PHP
PHP is a popular general-purpose scripting language that is used for web development.
PHP is recursive acronym for PHP: Hypertext Preprocessor
It was first called Personal Home Page by programmer Rasmus Lerdorf while writing code to maintain his own "personal home page."
Later, some Isreali developers formed PHP 3, changing the acronym to the recursive "PHP: Hypertext Preprocessor."
Its a lot like GNU in that the first letter actually stands for the acronym itself, Gnus Not Unix
Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world.
Who should learn PHP
PHP is one of the many server-side languages you can learn to build web sites. It joins other languages such as Java, ASP.NET, Ruby, and R. Eighty percent of the top 10 websites are powered by PHP, including Facebook and Wikipedia. PHP has consistently demonstrated its ability to scale the largest websites while at the same time having an easier learning curve than other languages. You should learn PHP and enhance your skills by hiring our MySQL/PHP Tutors.
How to Install PHP
visit Apache Friends and download XAMPP installer for windows or Linux.
Once installed click on start for Windows or in linux use the following command:
cd /opt/lampp
./xampp start
This will not only start the apache server but also the Mysql server if not installed
PHP with MYSQL for Database
Please refer to our post on how to setup MySQL database.
Connecting PHP with MySQL
PHP can connect to MySQL using either MySQLi or PDO connection;
MySQLi
MySQLi is Object Oriented but also offers a procedural API which is not supported by PDO
Syntax for Object Oriented MySQli
$servername = "localhost";
$username = "username";
$password = "password";
$con = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $con->connect_error);
}
echo "Connected successfully";
?>
Syntax for MySQLi Procedural
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
PDO
The biggest benefit of PDO is its error handling skills, on first error the code jumps to Exception and gives error message.
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=dbName", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
AJAX
We use AJAX to get the data from the server.
Allows for asynchronous data transfer - Getting the information from PHP might be time/resources expensive. Sometimes you just dont want to wait for the information, load the page, and have the information reach whenever.
AJAX has a latency issue as it creates an HTTP request, and HTTP requests are carried over the network and have network latencies.
Basic example
$.ajax({url: "urbantution.com/testAPI", success: function(result){
alert(result);
}});
This will alter the response you get from urbantution.com/testAPI which you may print in the HTML or alert to the user.
Top PHP frameworks
A framework is a set of tools and modules that can be reused for different projects. PHP frameworks mostly consist of MVC (Model View Controller) architecture.
A good framework not only helps with the scalability of the project but help write clean, efficient and reusable code.
Some of the most famous Framework are:-
- Laravel
- Symfony
- CodeIgniter
- Zend Framework / Laminas Project
- Yii (Framework)
- CakePHP
- Slim
- Phalcon
- FuelPHP
- Fat-Free Framework