Lesson 2

ບົດທີ 2: Syntax, Comment and Variable

<html>

<head></head>

<body>

<?php

echo'PHP Test';

?>

</body>

</html>

keywords ໃນພາສາ PHP (ເຊັ່ນ: if, else, while, echo, ແລະອື່ນໆ.), classes, functions, ແລະ user-defined functions ບໍ່ແມ່ນ case-sensitive. 

ຕົວຢ່າງ: ທັງສາມຕົວສະແດງຜົນແມ່ນເທົ່າກັນແລະຖືກຕ້ອງ

<!DOCTYPE html>

<html>

<body>

<?php

ECHO "Hello World!<br>";

echo "Hello World!<br>";

EcHo "Hello World!<br>";

?>

</body>

</html> 

ຕົວຢ່າງ 3 ຕົວປ່ຽນທີ່ແຕກຕ່າງກັນ

<!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> 

A comment ໃນຄໍາສັ່ງພາສາ PHP ແມ່ນພາກສ່ວນທີ່ ໂປຼແກຼມບໍ່່ດໍາເນີນການຣັນ. ມັນເປັນຄໍາອະທິບາຍສ່ວນຕ່າງໆ ທີ່ນັກຂຽນໂປຼແກຼມໄດ້ບັນທຶກໄວ້.

Comments ສາມາດຖືກນໍາໃຊ້ເພື່ອ:

<!DOCTYPE html>

<html>

<body>

<?php

// This is a single-line comment

# This is also a single-line comment

?>

</body>

</html> 

<!DOCTYPE html>

<html>

<body>

<?php

/*

This is a multiple-lines comment block

that spans over multiple

lines

*/

?>

</body>

</html> 

<!DOCTYPE html>

<html>

<body>

<?php

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

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

echo $x;

?>

</body>

</html> 

ຕົວປ່ຽນແມ່ນ Object ຫຼືສຶ່ງທີ່ນັກໂປຼແກຼມກໍານົດຂຶ້ນມາເພື່ອເກັບຂໍ້ມູນຫຼືເກັບຄ່າຕ່າງໆ ຂອງລະບົບ

ຄືກັນກັບພາສາອື່ນໆ, ການກໍານົດຕົວປ່ຽນໃນພາສາ php ປະກອບດ້ວຍ:

ການສະແດງຜົນຕົວປ່ຽນ ໃນພາສາ php ໄດ້ນໍາໃຊ້  Key Word ==> echo ແລ້ວຕາມດ້ວຍຕົວປ່ຽນ ເຊັ່ນຕົວຢ່າງ:

       ຕົວຢ່າງ 1:

<?php

$txt = "ບົດຮຽນ 2";

echo "I love $txt!";

?> 

ຕົວຢ່າງ2: 

<?php

$x = 5;

$y = 4;

echo $x + $y;

?> 

ຕົວຢ່າງ3: 

<?php

$txt = "ບົດຮຽນ 2";

echo "I love " . $txt . "!";

?> 

ຕົວຢ່າງ 1. 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>";

?> 

ຕົວຢ່າງ 2. 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>";

?> 

ຕົວຢ່າງ 3. Global ເພີ່ມເຕີມ

<?php

$x = 5;

$y = 10;

function myTest() {

  global $x, $y;

  $y = $x + $y;

}

myTest();

echo $y; // outputs 15

?> 

ຕົວຢ່າງ 4. $Global['index'];

<?php

$x = 5;

$y = 10;

function myTest() {

  $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];

}

myTest();

echo $y; // outputs 15

?> 

ຕົວຢ່າງ 4. 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:

<?php

function myTest() {

  static $x = 0;

  echo $x;

  $x++;

}

myTest();

myTest();

myTest();

?> 

ຄໍາຖາມຄົ້ນຄວ້າ