You are on page 1of 1
cg Declaration Velid name: $variable = 1; $.variable = ‘string $variable2 = erray() Explanation: * Begins a $ sign and followed by a letter or underscore, the rest of characters can be: letter, number, underscore * Case sensitive: Svar and $Varare different + Can'tbe named Sthis Predefined variables $.GET, $.POST, $ FILES, $ SESSION, $ COOKIE, dan SERVER $_GET, $_POST, and $_FILES to retrieve data submitted from html form, $_SESSION to retrieve session data that started with start_session), $ COOKIE for retrieve cookie data, $ SERVER for retrieve server information. Assign a value $nilai = 10; $nilai = $nilai + 5; echo $nilai; // 15; First, expressions on the right side will be evaluated, then the results are stored to the variable on the left side. Scope: GLOBAL & LOCAL $value - 10; // Global function print_value() { echo $value; // Undefined $value - 5; // Local (value not changed) echo $value; > print_value(); // 5 echo $value; // 10 Global: outside function, Local: inside a function. By default, global variable can't be used inside a function Variable Variables $value = 10; $ variable = ‘value’; echo $$variable; // 10 $$variabel same as $nilai Download pdf version @ WebDevZoom com Use curly braces to create a variable from a combination of strings and variables Svar = ‘foo $($var . ‘_value'} = 10; echo $foo_value; //19; other example: $list = array(“name” foreach ($list as $key $("input_”. $key} } echo $input_name; // Alfa echo $input_value; // 12 Decalaration “alfa”, “value” => 10); $val) { $val; Two ways to define constants: function or statement deFine(*BASE_URL', ‘webdevzoon. con’ const DB_NAME = ‘book_store’ ;// Statement echo BASE_URL; // webdevzoom.com Characteristics: © Constant’s value can't be changed, if the constant defined twice, it will trigger an error Constant name are case sensitive define() function can't be used inside Class © Before PHP 5.3, const statement can only be used within Class Only accept scalar (integer, float, etc.) and string values, starting from PHP 7 can accept array value Writing rule: ‘* Begin with a letter or an underscore (.), then followed by. letters, numbers, or underscore * Tips: todistinguish between others, use capital letter Declare 2 constant with an array (PHP 7) “localhost', define (*DB_CONFIG', [‘host* : > *3306", port" ‘db_name’ -> ‘webdevzoom' , ‘user’ => ‘root’, ‘pass’ => *" iy echo DB_CONFIS{ "host" }; ©2017 + WebDevZoom.com

You might also like