Basic PHP Syntax

PHP code is written within special tags that allow you to switch between HTML and PHP seamlessly. Here's an example of how PHP tags are used:

<?php
// PHP code goes here
?>

Primary Functions

PHP provides a rich set of built-in functions to perform various tasks. These functions make it easier to work with data and manipulate it. Here are some essential PHP functions:

echo and print

These functions are used to display output on a webpage. They are handy for showing text, variables, or the results of operations.

<?php
echo "Hello, World!";
$name = "John";
echo "My name is " . $name;
?>

strlen()

strlen() is used to determine the length of a string. It returns the number of characters in a string.

<?php
$length = strlen("Hello"); // $length will be 5
?>

strpos()

strpos() helps you find the position of a substring within a string. It returns the index where the substring is found, or false if it's not present.

<?php
$position = strpos("Hello, World!", "World"); // $position will be 7
?>

date()

date() allows you to format and display the current date and time in various ways.

<?php
$currentDate = date("Y-m-d H:i:s"); // $currentDate will contain the current date and time in the specified format
?>

Arrays in PHP

Arrays are used to store multiple values in a single variable. PHP supports two main types of arrays: indexed arrays and associative arrays.

Indexed Arrays

Indexed arrays use numeric indexes to access elements. The first element has an index of 0, the second has an index of 1, and so on.

<?php
$fruits = array("Apple", "Banana", "Orange");
echo $fruits[0]; // Outputs: Apple
?>

Associative Arrays

Associative arrays use named keys to access elements. Each element is paired with a key, and you can use that key to retrieve the value.

<?php
$person = array("name" => "John", "age" => 30, "city" => "New York");
echo "Name: " . $person["name"]; // Outputs: Name: John
?>

Includes and Requires

PHP allows you to break your code into reusable components using include and require statements. These statements let you incorporate external PHP files into your current script.

<?php
// Include an external file
include("functions.php");

// Use functions from the included file
$result = add(5, 3);
?>

With these foundational elements in PHP, you're equipped to start building dynamic web applications and websites. PHP offers a vast ecosystem of functions and libraries, so as you gain more experience, you'll be able to explore and expand your knowledge in web development further.