All posts by admin

PHP Variables

A variable is just a storage element.

Variables in PHP are represented by a dollar sign ( $ ) by the name of the variable. The variable name is case-sensitive.

The variable name should be start with letters ( a-zA-Z ) or underscore ( _ ) and it should not start with numeric /number

Below we have given some examples for your reference.

<?php
$x = 'hello';
$$x = 'world';
echo "$x ${$x}";
echo "$x $hello";
?>

Output:
hello world
hello world
<?php
class test {
    var $bar = 'I am bar.';
    var $arr = array('I am A.', 'I am B.', 'I am C.');
    var $r   = 'I am r.';
}

$test = new test();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $test->$bar . "\n";
echo $test->$baz[1] . "\n";

$start = 'b';
$end   = 'ar';
echo $test->{$start . $end} . "\n";

$arr = 'arr';
echo $test->$arr[1] . "\n";
echo $test->{$arr}[1] . "\n";

?>

Output:
I am bar.
I am bar.
I am bar.
I am r.
I am B.
<?php
$a = 10;
$b = 15;
echo $c = $a + $b;

function myTest()
{
global $a,$a;
$d=$a+$b;
}

myTest();
echo $d;

function myTest()
{
$GLOBALS['b']=$GLOBALS['a']+$GLOBALS['b'];
} 

myTest();
echo $b;

?>
Output:
25
25
25

PHP Echo / Print

In PHP there is 2 ways to print output:

  • echo
  • print

echo is bit faster compare with print.

echo can output one or more strings

print can only output one string, and it returns value as 1.

Echo

echo is a language construct, and can be used with or without parentheses: echo or echo().

<?php
$txt = "test data";
echo $txt1;
echo "$txt1";
echo '$txt1';
?>

Output:
test data
test data
$txt1

If we use double quotes in echo statement, it will check both the variable data and string.

If we use single quotes in echo statement,  it will consider all data as string only.

PHP Syntax

Any where we can use PHP script inside of the document.

The PHP script start  with <?php and ends with ?>

<?php
// PHP code goes here
?>

The default file extension is .php  ( Eg. test.php )

Example :

To get a fell of PHP code, first we will start  with simple PHP script. Since “Hello, World!” is an essential example, first we will create a friendly little “Hello, World!” script.

<html>
<head>
<title>Hello World</title>
<body>
    <?php echo "Hello, World!";?>
</body>
</html>

It will produce the following output

Hello, World!

Note: PHP statements are terminated by semicolon ( ; )

Comments in PHP:

A  comment in PHP code is a line, that is not execute or read by program. Below we mentioned the different types of comments in PHP.

<?php
     // Single line comment
     # single line comment
     /*
        Multiple line comment
     */
?>

Case-insensitive in PHP:

In PHP, all user defined function, classes and keywords ( Eg: print, echo, foreach, while, etc., ) are case-insensitive.

<?php
         print("test");
         PrInt("test");
         PRINT("test");
?>

Output:
test
test
test

PHP Introduction

Basic Knowledge

Before you are going to start PHP you should have a basic knowledge of the following :

  • HTML
  • CSS
  • Javascript

What is PHP ?

  • PHP :: Hypertext Preprocessor
  • PHP is server side scripting language
  • PHP is open source scripting language
  • PHP is free of cost and its free to download and use.

Why PHP ?

  • PHP runs almost on all of the platform like Windows, Linux, Unix, Mac OS X, etc.,
  • PHP is compatible with most of the web servers like Apache, IIS, mongodb, etc.,
  • PHP supports wide range of Databases like MySql,Sql,PgSql,etc.,
  • We can download PHP from www.php.net

Characteristics of PHP

  • Simplicity
  • Security
  • Efficiency
  • Familiarity
  • Flexibility

Execution

  • The PHP script is executed on the server, and the plain HTML result is sent back to the browser.

PHP Home

  • PHP –> Hypertext Preprocessor
  • PHP is a Server side scripting language
  • PHP is widely used and free of cost.
  • Before proceeding with this tutorial you should have the basic knowledge of computer programming , internet , Database and Mysql.
  • For testing you can use online editor compileonline.com