PHP for Beginners: The Basics
PHP Syntax and Variables
1 min read
Last updated 44 minutes ago
PHP runs on the server and produces output that the browser receives as HTML.
PHP tags
PHP code lives inside <?php ?> tags:
php
<?php
echo "Hello from PHP!";
Variables
Variables start with $ and don't need a declared type:
php
<?php
$name = "TutsFx";
$year = 2025;
$price = 19.99;
$isFree = true;
Data types
PHP has these core types:
php
$string = "Hello";
$integer = 42;
$float = 3.14;
$boolean = true;
$array = ['red', 'green', 'blue'];
$null = null;
String interpolation
Double-quoted strings interpolate variables:
php
<?php
$user = "Ada";
echo "Welcome back, {$user}!";
// Output: Welcome back, Ada!
Single quotes do not interpolate:
php
echo 'Welcome back, {$user}!';
// Output: Welcome back, {$user}!
Mixing PHP with HTML
html
<!DOCTYPE html>
<html>
<body>
<h1><?php echo "Dynamic heading"; ?></h1>
<p><?php echo date('Y'); ?></p>
</body>
</html>
!
Security first
Never trust user input. Anything from $_GET, $_POST or $_COOKIE must be escaped before output — use htmlspecialchars() or a framework's built-in escaping.
Debugging
Use var_dump() to inspect values while learning:
php
<?php
$items = ['a', 'b'];
var_dump($items);
Advertisement