Categories
PHP Tutorials

PHP Syntax- heredoc, string, variables and constants, logical operators

PHP Tutorial- Chapter 1 (PHP Syntax)

How to use HEREDOC in PHP with examples, also use easy to use examples in PHP for declaring variables, constants. Also know which tags you can use in PHP script to write PHP code in it.

HEREDOC <<< in PHP

It is used to write multi-line string –
<?php
$value = <<<XYZ
This is a string consisting of ‘ and “
XYZ;
echo $value;
?>

You can use single quote ‘ within a double quoted string. E.g
$variableName = “This is a string ‘ having single quote in it”;

Also you can use a double quote ” in a single quoted string. E.g
$variableName = ‘This is a string ” having double quote in it’;

AND (&&) –  OR  ( || )

You can use “and” instead of “&&” , also you can use “or” instead of “||“. If you use “and” or “or”, then the order of execution will be changed when these are used in any expression.

CONSTANTS in PHP:

Constants are named in CAPITAL LETTERs in PHP. Its starts with character / letter or underscore. Usage –
<?php
define(“CONSTANT_NAME”,”Bikram Choudhury”);
echo CONSTANT_NAME;
?>

How to define Variable in PHP :

Variable declaration with data type as it was in C is not required in PHP.
Usage :

<?php
$varname = 10;
$str = “Any string here”;
?>

Specifications for declaring variables:

* You donot need to use datatype like “int $varname = 10” as it was in C, Java etc.
* Variables starts with $ sign.
* After $ the 1st letter should be a letter or _
* Variables are case sensitive.
* In PHP5 variables are passed by reference and you donot need to use &$varname, in PHP4 variables was passed by reference in
function f(&$var) {
}

You can use the following tags in PHP –

Normal PHP tags –
<?php
?>

ASP type tags– to use this you need to edit PHP.INI file
<%
%>

Short Open Tags: Open php.ini file and edit “short open tags” configuration.
<?
?>

Also you can use
<script language=”PHP”>
…………………………..
…………………………..
</script>