PHP is a server-side programming language used extensively by WordPress. It helps generate webpage content, process requests, retrieve database information and implement dynamic website functionality.
PHP stands for PHP: Hypertext Preprocessor. It is a server-side scripting language that can generate HTML dynamically. WordPress uses PHP to build pages and implement many of its core features.
<?php
echo "Hello, World!";
?>
Output: Hello, World!
Save this example as a PHP file and run it using a PHP server, such as a local XAMPP installation.
When a visitor requests a WordPress page, the web server runs the necessary PHP code. WordPress loads the relevant data and template logic, then sends the generated response to the browser.
The browser receives the generated response, not the server's PHP source code.
PHP code is commonly written between
<?php and ?> tags.
A PHP file can contain both PHP code and HTML markup.
<!DOCTYPE html>
<html>
<body>
<h1>
<?php echo "Welcome to WordPress"; ?>
</h1>
</body>
</html>
PHP generates the heading text, and the browser displays it inside the HTML heading.
Variables store values that a PHP program can use. In PHP,
variable names begin with a dollar sign ($).
<?php
$institute = "Soopro Pathshala";
$course = "Web Development";
$duration = 6;
echo $institute;
echo "<br>";
echo $course;
echo "<br>";
echo $duration;
?>
Output:
PHP supports different data types for storing and working with information.
| Data Type | Example | Purpose |
|---|---|---|
| String | "Hello" |
Stores text. |
| Integer | 100 |
Stores whole numbers. |
| Float | 99.50 |
Stores decimal numbers. |
| Boolean | true |
Represents true or false. |
| Array | ["HTML", "CSS"] |
Stores multiple values. |
| Null | null |
Represents the absence of a value. |
Conditional statements allow PHP to make decisions based on whether a condition is true or false.
<?php
$fee = 5000;
if ($fee >= 5000) {
echo "Course fee is eligible for this offer.";
} else {
echo "Course fee is below the offer amount.";
}
?>
Output: Course fee is eligible for this offer.
Loops repeat a block of code. They are useful when displaying multiple values, such as a list of courses.
<?php
$courses = ["HTML", "CSS", "JavaScript", "PHP"];
foreach ($courses as $course) {
echo $course . "<br>";
}
?>
Output:
A function is a reusable block of code. PHP functions help organize code and avoid unnecessary repetition.
<?php
function welcomeStudent($name) {
return "Welcome, " . $name;
}
echo welcomeStudent("Rahul");
?>
Output: Welcome, Rahul
A theme's functions.php file is used to register
theme features and add theme-specific functionality. It is
loaded as part of the active theme.
<?php
function soopro_theme_setup() {
add_theme_support('title-tag');
}
add_action('after_setup_theme', 'soopro_theme_setup');
This example enables WordPress to manage the document title for the theme.
WordPress provides template functions that themes can use to display information dynamically.
| Function | Purpose |
|---|---|
bloginfo('name') |
Displays the site title. |
the_title() |
Displays the current post or page title in the loop. |
the_content() |
Displays the current post or page content in the loop. |
the_permalink() |
Displays the permalink for the current post in the loop. |
get_header() |
Loads the theme header template. |
get_footer() |
Loads the theme footer template. |
WordPress themes can use the Loop to retrieve and display posts. The following is a simplified example for a classic theme.
<?php
if (have_posts()) {
while (have_posts()) {
the_post();
?>
<h2>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
<div>
<?php the_content(); ?>
</div>
<?php
}
} else {
echo 'No posts found.';
}
?>
This code is intended for a suitable classic theme template. Block themes often use HTML templates and blocks instead of writing the Loop directly in a template file.
PHP code can access data and perform actions on the server. WordPress developers must validate input, check permissions and escape output appropriately.
Complete these tasks in a local PHP environment or test WordPress site:
functions.php file without modifying core files.functions.php file registers theme-specific functionality.Choose the correct answer for each question.