PHP Variable (PHP විචල්යය)

Variable යනු කිසියම් දත්තයක් හෝ තොරතුරක් අඩංගු කර තිබෙන හඳුන්වනයකි.
✔ නමක්,ගමක්,ස්ථානයක්,වයසක්,දෙයක් ආදී ඕනෑම දෙයක් PHP Variable එකක් ලෙස යෙදිය හැකිය.
✔ PHP Variables case-sensitive වේ.
✔ සෑම PHP Variable එකක්ම $ ලකුණින් ආරම්භ වේ.
✔ PHP Variable එකක් ඕනෑම ප්රමාණයක දිගකින් යුක්ත විය හැකිය.
✔ හිස්තැන් නොතිබිය යුතුය.
✔ Uppercase අක්ෂර lowercase අක්ෂර වලට සමාන නොවේ.
✔ අකුරකින් හෝ _ කින් ආරම්භ විය හැකිය.
✔ වම් පසින් අදාළ Variable එකද, එහි අර්ථ දැක්වීම (expression) වම් පසින්ද දැක්වෙන අතර එම දෙක වෙන් කිරීමට = භාවිතා කෙරේ.
Variables සඳහා උදාහරණ -
- $name
- $programming_1
- $_abc_variable
- $m
Variables සඳහා උදාහරණ කේතය -
<?php$variable=15;$variable_ab=38;$sum=$variable+$variable_ab;echo"First value=".$variable."<br>";echo"Second value=".$variable_ab."<br>";echo "sum=".$sum;?>
First value=15
Second value=38
sum=53
Variables ගොඩනැගිය හැකි ප්රධාන දත්ත ආකාර අටක් ඇත.
1. Integers − දශම ස්ථානය නැතිව සියලුම අංක මෙහිදී ඉදිරිපත් වේ. (උදා - 5432)
$int_var = 12345; $another_int = -12345 + 12345;
2. Doubles − ඉපිලෙන අංක හෙවත් floting point අංක ලෙස මේවා හැඳින්වේ. (උදා - 3.1590 හෝ 89.4)
<?php $many = 2.2888800; $many_2 = 2.2111200; $few = $many + $many_2; print("$many + $many_2 = $few <br>"); ?>
ප්රතිදානය
2.28888 + 2.21112 = 4.5
3. Booleans − මෙහි තිබෙන්නේ අගයන් දෙකක් පමණි. ඒවා නම් true or false හෙවත් සත්ය හෝ අසත්යය වේ.
if (TRUE) print("This will always print<br>"); else print("This will never print<br>");
4. NULL − එක අගයක් පමණක් ඇත. NULL හෙවත් නැත යන්න.
$my_var = NULL;
5. Strings − කිසියම් අගයක අනුලක්ෂණ ලෙස මෙය හැඳින්විය හැකිය.
<?php $variable = "name"; $literally = 'My $variable will not print!'; print($literally); print "<br>"; $literally = "My $variable will print!"; print($literally); ?>
My $variable will not print!\n My name will print
6. Arrays − මෙමඟින් වටිනාකම් කිහිපයක් ඉදිරිපත් කරයි.
7. Objects − විවිධ ගුණ සහ විධික්රම ඇතුලත් ආකාරයකි.
8. Resources − මෙය විශේෂ Variable ආකාරයක් වේ. එමඟින් වෙනත් බාහිර සම්පත් වලට සම්බන්ධක ලෙස යොදාගත හැකිය.
ඉහත සඳහන් කල දත්ත ආකාර පහත ආකාරයට වර්ගීකරණය කල හැකිය.
* Scalar Data Types - එක වටිනාකමක් පමණක් අඩංගු දත්ත විශේෂයක් වේ.
* Compound Data Types - එක අගයකට වඩා වැඩි දත්ත අගයන් පිහිටන අවස්ථා වේ.
* Special Data Types - ඉහත වර්ග දෙකට අයත් නොවන විශේෂ දත්ත වර්ග වේ.
Scope of Variables - Variable එකක් පවතී පවතින ප්රදේශය/සීමාව /විෂය පථය scope යන්නෙන් අර්ථවත් වේ.
scope ප්රධාන ආකාර දෙකකි.
1. Local scope - කිසියම් කාර්යයකට(function) අදාළව ඒ තුළම එහි scope (විෂයපථය) එක ඇතුලත් වේ නම් එය local scope ලෙස හැඳින්වේ.
උදාහරණ -
ප්රතිදානය -
Hello , welcome to php!
<?phpFunction concat() {$a = "Hello , ";$b = "welcome to php!";return $a . $b;}echo concat();/*$a can not used any Where out of this function*/?>
Hello , welcome to php!
2. Global scope - ඒ කාර්යයට අදාළ විෂය පථය පිටතින්, php කේතයේ ඕනෑම තැනක ඇතුලත් වීම global Variable ලෙස හැඳින්වේ.
<?php$global = "Welcome to php"."<br>";//its global variablefunction hello(){global $global;echo "$global "."<br>";}hello(); // Displays "Welcome to php"echo $global;//its accesseable any where in php scriptecho $global;hello();hello();?>
ප්රතිදානය -
Welcome to php
Welcome to php
Welcome to php



