PHP Syntax
PHP tags
The default file extension for PHP files is ".php
".
Full Tag Or Standard Tags
<?php and ?>
Syntex:
<?php
//one or more PHP statements
..
..
?>
Short tags
PHP allows a using a shorter representation of opening tag <? instead of cannonical usage <?php if it s enabled in php.ini file by enabling short_open_tag in php.ini file
<?
//one or more PHP statements
..
..
?>
PHP supports a short echo tag <?= which is equivalent to the more verbose <?php echo.
Example
Echo Tags, this type use there is no need to call echo
<?= "Hello World"; ?>
Output: Hello World
Comments in PHP
Single-line comments
<?php
// Hello testing
# This is a one-line shell-style comment
?>
Multiple-line comments:
<?php
/*
This is a multiple-lines comment
----
--
-
*/
?>
PHP Variables
Variables in a program are used to store some values or data that can be used later in a program. The variables are also like containers that store character values, numeric values, memory addresses, and strings.
- Any variables declared in PHP must begin with a dollar sign ($)
- A variable name can only contain alphanumeric characters and underscores (i.e., ‘a-z’, ‘A-Z’, ‘0-9, and ‘_’) in their name. Even it cannot start with a number.
- A constant is used as a variable for a simple value that cannot be changed. It is also case-sensitive
- One must keep in mind that variable names in PHP names must start with a letter or underscore and no numbers.
- PHP is a loosely typed language, and we do not require to declare the data types of variables, rather PHP assumes it automatically by analyzing the values. The same happens while conversion. No variables are declared before they are used. It automatically converts types from one type to another whenever required.
In this programme $txt, $x, $y are variables. which contains some values like sting, numeric and float
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
PHP Variables Scope
PHP has three different variable scopes:
- Local
- Global
- Static
Global and Local Scope
A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:
<?php
$x = 150; // global scope
function myPhp() {
// using x inside this function will generate an error
echo $x;
}
myPhp();
echo $x;
?>
Variable with local scope
<?php
function myPhp() {
$x = 5; // local scope
echo $x;
}
?>
out put : 5
Variable with Static
A static variable will not lose its value when the function exits and will still hold that value should the function be called again.
<?php
function myPHP() {
static $x = 10;
echo $x;
$x++;
}
myPHP();
myPHP();
myPHP();
?>
OutPut:
10
11
12
Related post
EknowledgePoint
Increase Your Knowledge with usEknowledgePoint is an innovative online learning platform that aims to democratize education by providing a vast array of courses and educational content to learners of all ages and backgrounds. It offers a diverse range of subjects, from academic topics to practical skills, making it a one-stop destination for anyone looking to expand their knowledge base.
5 comments
Knowledge is the fuel that powers our journey through life. It's platforms like EknowledgePoint that transform information into enlightenment, curiosity into competence, and dreams into reality. Embrace the opportunity to learn, and you'll find that the doors of possibility swing wide open. 🚀💡 #EknowledgePoint.
Thank you for your kind words! At EknowledgePoint, we are dedicated to empowering individuals on their learning journey. Your enthusiasm for knowledge and personal growth inspires us to keep providing the best educational resources and experiences. Let's continue to explore and learn together!.
Thanks For Your Support.
"I'm constantly amazed by the quality of instructors on EknowledgePoint. Learning from experts in their fields is both enlightening and empowering. 🌟👨🏫 #EknowledgePoint.
EknowledgePoint isn't just a platform; it's a community of learners. The support and camaraderie here are truly inspiring. 🤝🌍 #EknowledgePoint.