Part 3: Basic PHP Operations
Welcome back to our PHP programming tutorial series! 🎉 In Part 2, we explored PHP syntax and learned how to write and understand basic PHP code. Today, in Part 3, we will dive into basic PHP operations. We’ll cover arithmetic operations, string concatenation, and using constants. Let’s get started!
Arithmetic Operations in PHP
Arithmetic operations are used to perform calculations. PHP supports several arithmetic operators to handle different types of calculations.
Here are the basic arithmetic operators:
Operator | Description | Example |
---|---|---|
+ | Addition | $a + $b |
- | Subtraction | $a - $b |
* | Multiplication | $a * $b |
/ | Division | $a / $b |
% | Modulus (Remainder) | $a % $b |
Example Code for Arithmetic Operations:
<?php
$a = 15;
$b = 4;
// Addition
$sum = $a + $b;
echo "Addition: " . $sum . "<br>"; // Outputs: 19
// Subtraction
$difference = $a - $b;
echo "Subtraction: " . $difference . "<br>"; // Outputs: 11
// Multiplication
$product = $a * $b;
echo "Multiplication: " . $product . "<br>"; // Outputs: 60
// Division
$quotient = $a / $b;
echo "Division: " . $quotient . "<br>"; // Outputs: 3.75
// Modulus
$remainder = $a % $b;
echo "Modulus: " . $remainder . "<br>"; // Outputs: 3
?>
String Concatenation
String concatenation is used to join two or more strings together. In PHP, you use the .
operator to concatenate strings.
Example Code for String Concatenation:
<?php
$firstName = "Alice";
$lastName = "Smith";
// Concatenate strings
$fullName = $firstName . " " . $lastName;
echo "Full Name: " . $fullName . "<br>"; // Outputs: Alice Smith
// Adding more text
$message = $fullName . " is learning PHP!";
echo $message; // Outputs: Alice Smith is learning PHP!
?>
Working with Strings
Strings are sequences of characters, and PHP provides many functions to manipulate them. Here are some common string functions:
strlen()
: Returns the length of a string.
<?php
$text = "Hello, World!";
echo "Length of text: " . strlen($text); // Outputs: 13
?>
strtoupper()
: Converts a string to uppercase.
<?php
$text = "hello";
echo strtoupper($text); // Outputs: HELLO
?>
strtolower()
: Converts a string to lowercase.
<?php
$text = "HELLO";
echo strtolower($text); // Outputs: hello
?>
substr()
: Extracts a part of a string.
<?php
$text = "Hello, World!";
echo substr($text, 0, 5); // Outputs: Hello
?>
Constants in PHP
Constants are variables that cannot be changed once they are set. In PHP, you define constants using the define()
function. Constants are often used for values that remain the same throughout the script.
Example Code for Constants:
<?php
define("PI", 3.14); // Define a constant
echo "The value of PI is " . PI; // Outputs: The value of PI is 3.14
?>
Constants are case-sensitive by default, but you can make them case-insensitive by adding true
as a third parameter:
<?php
define("GREETING", "Hello World!", true); // Case-insensitive constant
echo greeting; // Outputs: Hello World!
?>
Basic Example
Here’s a simple example that combines arithmetic operations, string concatenation, and constants:
<!DOCTYPE html>
<html>
<head>
<title>Basic PHP Operations</title>
</head>
<body>
<?php
// Define a constant
define("PI", 3.14);
// Perform arithmetic operations
$radius = 5;
$circumference = 2 * PI * $radius;
// Concatenate strings
$message = "The circumference of a circle with radius " . $radius . " is " . $circumference . ".";
// Display the message
echo $message;
?>
</body>
</html>
Summary
In Part 3, we learned about basic PHP operations including arithmetic calculations, string concatenation, and working with constants. These fundamental concepts will help you build more complex PHP applications as you continue learning.
What’s Next?
In Part 4, we’ll dive into Working with Arrays in PHP. We’ll explore different types of arrays and how to manipulate them to store and manage data.
Homework
- Experiment: Create a PHP file and practice using different arithmetic operators and string functions.
- Practice: Write a PHP script that calculates the area of a rectangle and displays the result.
Feel free to leave comments if you have any questions or run into any issues. Happy coding! 🚀
Next Part Teaser
Stay tuned for Part 4: Working with Arrays, where we will explore indexed arrays, associative arrays, and multidimensional arrays in PHP!
Additional Resources
If you want to explore more about PHP operations, check out these resources: