Sunday, 28 April 2024

PHP Echo / Print

 


PHP Echo

Simply, the echo statement is used to output data. In most cases, anything output by echo ends up viewable in the browser. You could also have used the print()  function in place of the echo statement. Using echo or print() is a  matter of taste; when you look at other people’s scripts, you might see either use.

PHP Print

Whereas echo is a statement, print() is a function. A function is a command that performs an action, usually modified in some way by data provided for it. Data sent to a function is almost always placed in parentheses after the function name. In this case, you could have sent the print() function a collection of characters or a string. Strings must always be enclosed in quotation marks, either single or double.

For example:

Difference Between Echo and Print

#EchoPrint
Parametersecho can take more than one parameter when used without parentheses.The syntax is echo expression [ expression[ expression] ]. Note that echo ($arg1 $arg2) is invalid.Print only takes one parameter.
Return valueEcho does not return any value.Print always returns 1 (integer).
Syntaxecho ( string $arg1 [ string $arg2] )Int print ( string $arg )
What is it?In PHP echo is not a function but a language construct.In PHP print is not a really function but a language construct. However, it behaves like a function in that it returns a value.”

PHP Variables

 



What is PHP Variables?

A variable is a place  where you can store things, such as  number, a date  or some text. You can put the text or number into variables so you can retrieve them later or so you can manipulate them. Variables  are called variables because the value that they can vary. 

Variable Naming Conventions and Best practices

Variables in PHP are started with dollar($) sign followed by the name of the variable. The Variable name is case-sensitive, meaning that PHP would treat $name and $Name as two different variables.

PHP Variables Naming Rules
  • Name must begin with a $ dollar sign, for example, $name
  • Name can comprise letters, numbers, and  underscore characters, but not spaces – for  example, $subtotal_1
  • The first character after the $ dollar sign must be a letter or an underscore character –  it cannot be a number.

Note: that variable names in PHP are case-sensitive so $name, $Name, and $NAME are three separate individual variables.

PHP variables are loosely typed meaning they can contain data of any type, unlike  “strongly typed” variables in some languages where the data type must be specified when the variables are created. So, PHP variables may happily contain an integer, or a floating-point number, or a string of text characters, or a Boolean value of True or FALSE, or an object, or a NULL empty value.

Do not confuse the purpose of double and single quotes. Remember that PHP only makes variables substitutions for mixed strings enclosed with double quotes.

 A variable is created in PHP script simply by stating its name. The variable can then be assigned an initial value (initialized) by using the = assignment operator to state its value. This statement, and all others in PHP,  must end with a semi-colon like this:

The value contained within the variable can then be displayed by referencing it using the variable like this :

Adding Comments to PHP Code

 



What is comment?

A comment is a text in a script that is ignored by the PHP engine. Comments can be used to make the code more readable or to annotate a script.

Single Line Comment

Single-line comments begin with two forward slashes (//) or
a single hash sign(#).
The PHP engine ignores all text between these marks and either the end of the line or the PHP close tag:
// this is a comment
# this is another comment

Multiple Line Comment

Multiline comments begin with a forward slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/):
/*
this is a comment none of this will be parsed by the PHP engine
*/

Introduction to PHP



 What is PHP?

PHP is a server-side scripting language designed specifically for the web. Within an HTML page, you can embed PHP code that will be executed each time the page is visited. Your PHP code is interpreted at the web server and generates HTML or other output that the visitor will see.
A PHP file is just an HTML file saved using a .php extension instead of a .html or .htm extension, which tells the server to looking the page for code.
PHP is a language that can be used to create dynamic web pages, In fact that is the whole point of PHP.

Static Vs Dynamic web pages

A static web page never changes, unless a person specifically edit the page.
A dynamic web page can be different every time it is viewed by a browser because the server edits the page prior to sending it to the browser accordingly to what instructions the programmer has coded into that specific page.
PHP was introduced in 1994. As of November 2007, it was installed on more than 21 million domains worldwide, and this number is growing rapidly. You can see the current number at http://www.php.net/usage.php
PHP is an Open Source project. PHP originally stood for Personal Home Page and now stands for PHP Hypertext Preprocessor.

A Simple PHP Script

Output: Hello Web!

Beginning and Ending a Block of PHP Statements

When writing PHP, you need to inform the PHP engine that you want it to execute your commands. If you don’t do this, the code you write will be mistaken for HTML and will be output to the browser. You can designate your code as PHP with special tags that mark the beginning and end of PHP code blocks.
shows four such PHP delimiter tags.

Tag StyleStart TagEnd Tag
Standard tags<?php?>
Short tags<??>
ASP tags<%%>
Script tags<SCRIPT LANGUAGE=”php”></SCRIPT>

Example 1  :

Example 2 :

Example  3 :

Example 4 :

You can also put single lines of code in PHP on the same line as the PHP start and end tags:

Combining HTML and PHP

A PHP Script Incorporated into HTML

As you can see, incorporating PHP code into a predominantly HTML document is simply a matter of typing in the code. The PHP engine ignores everything outside the PHP open and close tags.
You can include as many blocks of PHP code as you need in a single document, interspersing them with HTML as required. Although you can have multiple blocks of code in a single document, they combine to form a single script. Any variables defined in the first block will usually be available to subsequent blocks.