0% found this document useful (0 votes)
43 views

Learn PHP

The document contains code snippets demonstrating basic PHP concepts like: 1. Using echo to output text and variables 2. Including PHP code within HTML tags 3. Defining and using variables of different data types 4. Comments in PHP code 5. Functions and variable scopes The snippets show the basic syntax of PHP and how it can be used to output dynamic content, manipulate variables, and write simple scripts within an HTML document structure.

Uploaded by

Nisa Rezuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Learn PHP

The document contains code snippets demonstrating basic PHP concepts like: 1. Using echo to output text and variables 2. Including PHP code within HTML tags 3. Defining and using variables of different data types 4. Comments in PHP code 5. Functions and variable scopes The snippets show the basic syntax of PHP and how it can be used to output dynamic content, manipulate variables, and write simple scripts within an HTML document structure.

Uploaded by

Nisa Rezuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

1.

<!DOCTYPE html>

<html>

<body>

<?php

$txt = "PHP";

echo "I love $txt!";

?>

</body>

</html>

I love PHP!

2.

<!DOCTYPE html>

<html>

<body>

<h1>My first PHP page</h1>

<?php

echo "Hello World!";

?>

<body>

<html>
My first PHP page
Hello World!

3.

<!DOCTYPE html>

<html>

<body>

<?php

ECHO "Hello World!<br>";

echo "Hello Wolrld<br>";

EcHo "Hello World<br>";

?>

</body>

</html>

Hello World!
Hello Wolrld
Hello World

4.

<!DOCTYPE html>

<html>

<body>

<?php
$color = "red";

echo "My car is " . $color . "<br>";

echo "My house is " . $color . "<br>";

echo "My boat is ". $color . "<br>";

?>

</body>

</html>

My car is red
My house is red
My boat is red

5.
<form action="welcome.php" method="post">

First name: <input type="text" name="fname">

</form>

<html>

<body>

Welcome <?php echo

$_POST["fname"]

; ?>

</body>

</html>
First name: 
Welcome

6.

<!DOCTYPE html>

<html>

<body>

<?php

echo date(

"Y.m.d"

);

?>

</body>

</html>

2021.05.17

7.

<!DOCTYPE html>

<html>

<body>

<?php

//This a single linecomment


# This is also a single line comment

?>

</body>

</html>

8.

<!DOCTYPE html>

<html>

<body>

<?php

// u can also use comments to leave out parts of a code line

$x = 5 /* + 15 */ +5

echo $x;

?>

</body>

</html>

10

9.

<!DOCTYPE html>

<html>

<body>

<?php

$txt = "<br>"."Hello world!";

$x = 5;
$y = 10.5;

echo $txt;

echo "<br>";

echo $x;

echo "<br>";

echo $y;

?>

Hello world!
5
10.5

10.

<!DOCTYPE html>

<html>

<body>

<?php

$txt = "W3Schools.com";

echo "I love $txt!";

?>

</body>

</html>

<?php

$txt = "W3SCHOOLS.COM";

echo " <br>I love $txt!";


?>

I love W3Schools.com!
I love W3SCHOOLS.COM!

11.

<!DOCTYPE html>

<html>

<body>

<?php

$x = 5;

$y = 4;

echo $x + $y;

?>

<?php

$x = 3;

$y = 2;

echo $x +$y;

?>

</body>

</html>

95

12.
PHP Variables Scope
In PHP, variables can be declared anywhere in the script.

The scope of a variable is the part of the script where the variable can be
referenced/used.

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:

Example
Variable with global scope:

<?php
$x = 5; // global scope

function myTest() {
  // using x inside this function will generate an error
  echo "<p>Variable x inside function is: $x</p>";
}
myTest();

echo "<p>Variable x outside function is: $x</p>";


?>

Variable x inside function is:

Variable x outside function is: 5


A variable declared within a function has a LOCAL SCOPE and can only be
accessed within that function:

Example
Variable with local scope:

<?php
function myTest() {
  $x = 5; // local scope
  echo "<p>Variable x inside function is: $x</p>";
}
myTest();

// using x outside the function will generate an error


echo "<p>Variable x outside function is: $x</p>";
?>

Variable x inside function is: 5

Variable x outside function is:

13.

PHP The static Keyword


Normally, when a function is completed/executed, all of its variables are
deleted. However, sometimes we want a local variable NOT to be deleted. We
need it for a further job.

To do this, use the static keyword when you first declare the variable:

Example
<?php
function myTest() {
  static $x = 0;
  echo $x;
  $x++;
}

myTest();
myTest();
myTest();
?>
0
1
2
3

14.

<!DOCTYPE html>
<html>
<body>

<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>

<?php
echo "<h2>PHP IS FUN</h2>";
echo "Hello world!<br>";
echo "im about to learn php!<br>";
echo "this ", "string ", "was ", "made ", "with multiple parameters.";
?>

</body>
</html>
PHP is Fun!
Hello world!
I'm about to learn PHP!
This string was made with multiple parameters.

PHP IS FUN
Hello world!
im about to learn php!
this string was made with multiple parameters.

15.
<?php
$txt1 = "learn php";
$txt2 = "w3schools.com";
$x = 5;
$y = 4;

echo "<h2>".$txt1."</h2>";
echo "Study php at ".$txt2."<br>";
echo $x + $y;
?>

learn php
Study php at w3schools.com
9

16.

<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>

PHP is Fun!
Hello world!
I'm about to learn PHP!

17.

PHP Data Types


Variables can store data of different types, and different data types can do
different things.

PHP supports the following data types:

 String
 Integer
 Float (floating point numbers - also called double)
 Boolean
 Array
 Object
 NULL
 Resource

PHP String
A string is a sequence of characters, like "Hello world!".

A string can be any text inside quotes. You can use single or double quotes:

Example
<?php
$x = "Hello world!";
$y = 'Hello world!';

echo $x;
echo "<br>";
echo $y;
?>

Try it Yourself »

PHP Integer
An integer data type is a non-decimal number between -2,147,483,648 and
2,147,483,647.

Rules for integers:

 An integer must have at least one digit


 An integer must not have a decimal point
 An integer can be either positive or negative
 Integers can be specified in: decimal (base 10), hexadecimal (base
16), octal (base 8), or binary (base 2) notation

In the following example $x is an integer. The PHP var_dump() function


returns the data type and value:

Example
<?php
$x = 5985;
var_dump($x);
?>
Ans:
5985

18.

PHP Object
Classes and objects are the two main aspects of object-oriented
programming.

A class is a template for objects, and an object is an instance of a class.


When the individual objects are created, they inherit all the properties and
behaviors from the class, but each object will have different values for the
properties.

Let's assume we have a class named Car. A Car can have properties like
model, color, etc. We can define variables like $model, $color, and so on, to
hold the values of these properties.

When the individual objects (Volvo, BMW, Toyota, etc.) are created, they
inherit all the properties and behaviors from the class, but each object will
have different values for the properties.

If you create a __construct() function, PHP will automatically call this


function when you create an object from a class.

Example
<?php
class Car {
  public $color;
  public $model;
  public function __construct($color, $model) {
    $this->color = $color;
    $this->model = $model;
  }
  public function message() {
    return "My car is a " . $this->color . " " . $this->model . "!";
  }
}

$myCar = new Car("black", "Volvo");
echo $myCar -> message();
echo "<br>";
$myCar = new Car("red", "Toyota");
echo $myCar -> message();
?>

my car is a black volvo!


my car is a red Toyota!

19.

PHP String Operators


PHP has two operators that are specially designed for strings.
Operator Name Example Result

. Concatenation $txt1 . $txt2 Concatenation


$txt2

.= Concatenation assignment $txt1 .= $txt2 Appends $txt2

Concatenation= string joined together

You might also like