Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

PHP Basics: Variables, Data Types, Operators, Control Structures, and Functions

Tech 1

php"><?php $value1 = 1111; $value2 = 2222; $value2 = &$value1; $value1 = 8888; echo $value2; // Outputs 8888 \nVariables in PHP start with a $ symbol. Variable names must begin with a letter or an underscore, can only contain letters, numbers, and underscores, cannot have spaces, and are case-sensitive.\nphp"><?php // Example of comments // Single line comment /* Multi-line comment */ // Variables $name = "John Doe"; $age = 30; $height = 175.5; $number = 100; $number1 = 101; // Variable operations $sum = $number + $number1; echo $sum; // Assignment $new_name = $name; echo "<br>"; echo $new_name; $name = "New John Doe"; echo "<br>"; echo $new_name; echo "<br>"; echo $name; echo "<br>"; // Reference assignment $new_name2 = &$name; echo "new_name2: ". $new_name2; echo "<br>"; $name = "Changed to Jane Doe"; echo "new_name2: ". $new_name2; echo "<br>"; $new_name2 = "Hello"; echo "name: ". $name; // Variable variables $name = "abc"; $abc = "def"; $def = "123456789"; $test = $$name; echo $test; echo "<br>"; // var_dump for more information $bool = false; echo $bool; echo "<br>"; var_dump($bool); echo "<br>"; // isset checks if a variable is set $names = "123"; var_dump(isset($names)); var_dump(isset($names1)); $is_exists = isset($names); if ($is_exists) { echo "Variable exists"; } else { echo "Variable does not exist"; } // unset to delete a variable unset($names); var_dump(isset($names)); // Global variables var_dump($_SERVER); var_dump($_COOKIE); var_dump($_SESSION); var_dump($_GET); var_dump($_POST); var_dump($_REQUEST); // Constants define('CONST_NAME', 'Value'); echo CONST_NAME; // Data types $int_val = 100; var_dump($int_val); $float_val = 100.24; var_dump($float_val); // Strings $str = "String $int_val"; $str1 = 'String $int_val'; var_dump($str); var_dump($str1); // Arrays $arr = [1, 3, 4, 5, 6]; var_dump($arr); $arr2 = array(1, 3, 4, 5, 6); var_dump($arr2); $arr3[1] = 100; var_dump($arr3); // Objects class Person { public $name = "123"; } $person = new Person(); var_dump($person); // Special types $test = NULL; var_dump($test); $fd = fopen('./abc.txt', 'r'); var_dump($fd); ?> \n## Type Conversion and Operators php"><?php // Type conversion $num = 100; $float_val = 20.01; var_dump($num + $float_val); $bool = true; $num2 = 10; var_dump($bool + $num2); $bool2 = false; var_dump($bool2 + $num2); $str = "100.01aaaa"; $num3 = 10; var_dump($str + $num3); $bool3 = true; $str2 = "10abc"; var_dump($bool3 + $str2); // Explicit type conversion $str = "101abc"; var_dump(intval($str)); var_dump(floatval($str)); var_dump(boolval($str)); $num4 = 100; var_dump(boolval($num4)); // Type checking $bool4 = true; $str3 = "abcdef"; var_dump(is_bool($bool4)); var_dump(is_bool($str3)); // Arithmetic operators $num5 = 100; $num6 = 50; var_dump($num5 + $num6); var_dump($num5 - $num6); var_dump($num5 * $num6); var_dump($num5 / $num6); var_dump($num5 % 3); var_dump($num5 + $num6 * 2); var_dump(($num5 + $num6) * 2); $num5++; var_dump($num5); $num5--; var_dump($num5); // String operators $name = "John"; $age = 23; echo "Name: ". $name . " Age: ". $age; echo $name . $age; // Assignment operators $name = "John Doe"; var_dump($name); $num = 100; $num += 100; var_dump($num); $num -= 50; var_dump($num); $num *= 2; var_dump($num); $num /= 3; var_dump($num); $num %= 3; var_dump($num); $str = "John Doe"; $str .= " Age 23"; var_dump($str); // Comparison operators $num = 100; $num1 = 200; var_dump($num > $num1); var_dump($num < $num1); var_dump($num >= 100); var_dump($num <= 100); var_dump($num == $num1); var_dump($num == 100); $str = "100aaa"; var_dump($num == $str); var_dump($num === $str); // Ternary operator var_dump(100 > 2 ? "True" : "False"); var_dump(100 < 2 ? "True" : "False"); var_dump($num < $num1 ? "True" : "False"); // Logical operators var_dump(1 > 2 and 3 > 1); var_dump(2 > 1 and 3 > 2); var_dump(1 > 2 or 3 > 1); var_dump(2 > 1 or 3 > 2); var_dump(2 < 1 or 3 < 2); var_dump(!(1 > 2)); var_dump(!(2 > 1)); ?> \n## Control Structures php"><?php // Sequential structure echo "1"; echo "2"; echo "3"; // Conditional structure if (1 < 2) { echo "Condition met"; } echo "<br>"; if (2 < 1) { echo "Condition not met"; } echo "<br>"; // Two-way branch if (2 > 3) { echo "Condition met"; } else { echo "Condition not met"; } echo "<br>"; $age = 17; if ($age >= 18) { echo "Play freely"; } else { echo "Under 18, limit playtime"; } echo "<br>"; // Multi-branch $age = 35; if ($age < 18) { echo "Under 18, limit playtime"; } elseif ($age <= 30) { echo "Play freely"; } elseif ($age <= 50) { echo "Consider spending some money!"; } else { echo "Take care of your health, go to bed early!"; } echo "Branches end!"; // Loop structures for ($i = 0; $i < 10; $i++) { echo $i . "<br>"; } // Nested loops for ($i = 1; $i < 10; $i++) { for ($j = 1; $j <= $i; $j++) { echo $i . "*" . $j . "=" . ($i * $j) . " "; } echo "<br>"; } $num = 1; while ($num <= 10) { $num++; if ($num == 5) { continue; } echo $num; } $num = 1; do { echo "do-while loop"; echo $num; } while ($num > 10); $arr = ['a', 'b', 'c', 'd', 'e', 'f']; foreach ($arr as $k => $v) { echo "Key: ". $k . " Value: ". $v . "<br>"; } ?> \n## Functions php"><?php // Function declaration function func() { echo "I am a function<br>"; } echo "Outside function body"; // Function call echo "Before function call<br>"; func(); echo "After function call<br>"; // Parameters and return values function func1($num, $num2) { echo $num + $num2; } $num = 100; func1($num, 200); // Return value function func2($num, $num1) { return $num + $num1; } $result = func2(100, 200); var_dump($result); // Default parameters function func3($num, $num2 = 100) { return $num + $num2; } $num = 110; $result = func3($num); var_dump($result); $result = func3($num, 500); var_dump($result); // Scope $name = "John Doe"; function func4() { $realname = "Jane Doe"; var_dump("Real name inside function: ". $realname); } function func5() { $num = 100; var_dump("Num inside function: ". $num); } function func6() { global $name; var_dump("Name in func6: ". $name); $name = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; } func4(); func5(); func6(); var_dump("Name outside: ". $name); // Static variables function func7() { static $num = 1; $num++; echo "<br>"; var_dump($num); } func7(); func7(); func7(); func7(); func7(); // Variable functions $func_name = "func7"; $func_name(); ?> \n## Arrays php"><?php // Array initialization $arr = array('dong', '18', 'Security Expert'); $arr1[] = 'x'; echo $arr[2]; echo $arr1[0]; // Output: Security Expert x var_dump($arr); // Array traversal for ($i = 0; $i < count($arr); $i++) { echo $arr[$i] . '<hr />'; } // Associative array $arr = array('name' => 'dong', 'age' => '18', 'profession' => 'Security Expert'); foreach ($arr as $key => $value) { echo 'Key: ' . $key . ' Value: ' . $value . '<br />'; } // Multidimensional array $arr = array('name' => 'dong', 'age' => '18', 'profession' => array('Security', 'Network Engineering')); foreach ($arr as $key => $value) { if (is_array($value)) { foreach ($value as $k1 => $v1) { echo 'Nested array'; } } else { echo 'Key: ' . $key . ' Value: ' . $value . '<br />'; } } ?> \n## Timestamps php"><?php // Timestamp $timestamp = time(); echo $timestamp . '<hr />'; echo date('Y-m-d H:i:s', $timestamp); // Output: 1591749201 2020-06-10 08:33:21 ?> \n## GET and POST Requests php"><?php // GET request var_dump($_GET); // Write GET parameter to file $l = $_GET['mumu']; $m = fopen("./22.txt", "w"); fwrite($m, $l); ?> \n## Ternary Operator php"><?php // Ternary operator example $a = 1 ? "2343" : "300"; // Outputs 2343 $a = isset($_GET['name']) ? '1000' : $_GET['name']; echo $a; ?>

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.