2014. szeptember 10., szerda

Változók típusainak megváltoztatása, settype()

Megváltoztathatjuk egy változó típusát a settype() függvénnyel/utasítással

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 4.2 Changing the Type of a Variable with settype()</title>
</head>
<body>
<div>
<?php
$undecided = 3.14;
print gettype( $undecided ); // double
print " -- $undecided<br />";  // 3.14
settype( $undecided, string );
print gettype( $undecided ); // string
print " -- $undecided<br />";  // 3.14
settype( $undecided, integer );
print gettype( $undecided ); // integer
print " -- $undecided<br />";  // 3
settype( $undecided, double );
print gettype( $undecided ); // double
print " -- $undecided<br />";  // 3.0
settype( $undecided, boolean );
print gettype( $undecided ); // boolean
print " -- $undecided<br />";  // 1
?>
</div>
</body>
</html>


Eredmény:

double -- 3.14
Notice: Use of undefined constant string - assumed 'string' in E:\STY PHP in 24 Hours\Hour 04\listing4.2.php on line 14 string -- 3.14
Notice: Use of undefined constant integer - assumed 'integer' in E:\STY PHP in 24 Hours\Hour 04\listing4.2.php on line 17 integer -- 3
Notice: Use of undefined constant double - assumed 'double' in E:\STY PHP in 24 Hours\Hour 04\listing4.2.php on line 20 double -- 3
Notice: Use of undefined constant boolean - assumed 'boolean' in E:\STY PHP in 24 Hours\Hour 04\listing4.2.php on line 23 boolean -- 1

Vegyük észre, hogy a konverzió közben értékvesztés jöhet létre!

Nincsenek megjegyzések:

Megjegyzés küldése