Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

PHP Fundamentals: Basic Syntax and Core Concepts

Tech May 8 3

PHP (PHP: Hypertext Preprocessor) is a widely-used open-source scripting language. PHP files can contain text, HTML, JavaScript, and PHP code. PHP code executes on the server, and results return to browsers as plain HTML. The default file extension for PHP files is .php.

PHP scripts start with <?php and end with ?>. Variables start with a $ symbol, followed by the name. Variable names must begin with a letter or underscore, only include alphanumeric characters and underscores, and cannot contain spaces.

<?php
    $greeting = "Hello PHP";
    echo $greeting;
?>

PHP supports several data types: String, Integer, Float, Boolean, Array, Object, and NULL. Use var_dump() to check data types.

<?php
$stringVal = "Text data type";
$intVal = 5678;
$floatVal = 3.14159;
$expVal = 2E4;
$boolVal = false;
$arrVal = array("X", "Y", "Z");
class SampleObject {
    var $property;
    function method() {}
}
$objVal = new SampleObject();
$nullVal = NULL;
var_dump($stringVal, $intVal, $floatVal, $expVal, $boolVal, $arrVal, $objVal, $nullVal);
?>

Constants are fixed values that can't change in a script. Define them with define(), which takes three parameters: name, value, and optional case-insensitivity flag.

<?php
define("APP_TITLE", "Global Application Title", true);
echo APP_TITLE;
echo app_title;
?>

PHP uses . to concatenate strings. Key string functions include strlen() (length), strpos() (find position), str_replace() (replace text), and trim() (remove whitespace).

<?php
$str1 = "good";
$str2 = "morning";
echo $str1 . " " . $str2 . "<br>";
echo "Length of str1: " . strlen($str1) . "<br>";
echo "Position of 'o' in str1: " . strpos($str1, 'o') . "<br>";
$modified = str_replace('o', 'a', $str1);
echo "Modified str1: " . $modified . "<br>";
$whitespaceStr = "   Trim this string   ";
echo "Trimmed string: " . trim($whitespaceStr);
?>

Control statements include conditional statements (if, if...else, if...elseif...else, switch) and loops (for, while, do...while, foreach). break terminates a loop, continue skips to the next iteration.

<?php
$score = 85;
if ($score >= 90) {
    echo "Excellent";
} elseif ($score >= 80) {
    echo "Good";
} else {
    echo "Needs improvement";
}
$numbers = array(1, 2, 3, 4, 5);
foreach ($numbers as $num) {
    if ($num == 3) continue;
    echo "Number: " . $num . "<br>";
}
?>

Define custom functions with function keyword. Functions can take parameters, have default parameter values, and return values. Use return to output results.

<?php
function greetUser($name = "Guest") {
    return "Hello, " . $name . "!";
}
echo greetUser("Alice") . "<br>";
echo greetUser();
?>

Variablee scope in PHP includes local (function-level), global (script-level), and static (persistent local variables). Use global to access global variables inside functions, and static to preserve variables after function execution.

<?php
$globalVar = 10;
function testScope() {
    global $globalVar;
    static $count = 0;
    $count++;
    echo "Count: " . $count . "<br>";
    echo "Global var: " . $globalVar . "<br>";
}
testScope();
testScope();
testScope();
?>

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.