revision:
code: <?php $a = "Hello world!"; $b = 'Hello world!'; echo $a; echo "<br>"; echo $b; ?> <br><br> <?php $c = 5985; var_dump($c); echo "<br>"; $d = 10.365; var_dump($d); echo "<br>"; $cars = array("Volvo","BMW","Toyota"); var_dump($cars); ?> <br><br> <?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(); ?> <br><br> <?php $e = "Hello world!"; $e = null; var_dump($e); ?>
code: <?php $is_email_valid = false; var_dump($is_email_valid); echo "<br>"; $is_submitted = true; var_dump($is_submitted); ?>
code: <?php $title = "PHP string is awesome"; echo strlen($title)."<br>"; echo $title[0]."<br>"; echo $title[11]."<br>"; echo strlen("What a wonderful world!"); echo "<br>"; echo str_word_count("What a wonderful world!"); echo "<br>"; echo strrev("What a wonderful world!"); echo "<br>"; echo strpos("What a wonderful world!", "world"); echo "<br>"; echo str_replace("world", "place", "What a wonderful world!"); echo "<br>"; $name = "Walter"; echo "Hello {$name}"; ? >
code: <?php $email = null; var_dump($email); echo "<br>"; var_dump(is_null($email)); echo "<br>"; $home = "www.lwitters.com"; var_dump(is_null($home)); ?>
code: <y;?php // Check if the type of a variable is integer $f = 5985; var_dump(is_int($f)); echo "< br>"; // Check again... $f = 59.85; var_dump(is_int($f)); // Check if the type of a variable is float echo "< br>"; $g = 10.365; var_dump(is_float($g)); echo "< br>"; // Check if a numeric value is finite or infinite $h = 1.9e411; var_dump($h); echo "< br>"; // Invalid calculation will return a NaN value $j = acos(8); var_dump($j); echo "< br>"; // Check if the variable is numeric $k = 5985; var_dump(is_numeric($k)); echo "< br>"; $l = "5985"; var_dump(is_numeric($l)); echo "< br>"; $m = "59.85" + 100; var_dump(is_numeric($m)); echo "< br>"; $n = "Hello"; var_dump(is_numeric($n)); echo "< br>"; // Cast float to int $o = 23465.768; $int_cast = (int)$o; echo $int_cast; echo "< br>"; // Cast string to int $p = "23465.768"; $int_cast = (int)$p; echo $int_cast; ?>
code: <?php echo(pi()); echo "< br>"; print(pi()); echo "< br>"; echo(min(0, 150, 30, 20, -8, -200) . "< br>"); echo(max(0, 150, 30, 20, -8, -200) . ":< br>"); echo(abs(-6.7) . "< br>"); echo(sqrt(64) . "< br>"); print(round(0.60) . "< br>"); echo(round(0.49) . "< br>"); echo(round(0.50) . "< br>"); echo((rand()) . "< br>"); echo(rand(10, 100) . "< br>"); ?>
Type casting allows you to convert a value of one type to another. To cast a value, use the following type casting operators:
(array): array;
(bool) or (boolean): boolean;
(int) or (integer): integer;
(object): object;
(real), (double), or (float): float;
(string): string.
code: <?php echo (int)12.5 . '<br>'; // 12 echo (int)12.1 . '<br>'; // 12 echo (int)12.9 . '<br>'; // 12 echo (int)-12.9 . '<br>'; // -12 $message = 'Hi'; $num = (int) $message; echo $num."<br>"; // 0 $amount = (int)'100 USD'; echo $amount."<br>"; // 100 $qty = null; echo (int)$qty."<br>"; // 0 $amount = (float)100; echo $amount."<br>"; // 100 $amount = 100; echo (string)$amount . " USD"."<br>"; // 100 USD $amount = 100; echo $amount . ' USD'."<br>"; // 100 USD $is_user_logged_in = true; echo (string)$is_user_logged_in."<br>"; // 1 $numbers = [1,2,3]; $str = (string) $numbers; echo $str."<br>"; // Array ?>
PHP is a loosely typed programming language: when you define a variable, you don't need to declare a type for it. Internally, PHP will determine the type by the context in which you use the variable.
PHP has a feature called type juggling, which means that during the comparison of variables of different types, PHP will convert them to the common, comparable type.
code: <?php $qty = 20; if($qty == '20') { echo 'Equal'; } print "<br>"; $total = 100; $qty = "20"; $total = $total + $qty; echo $total; // 120 print "<br>"; ?>