w3resource

php.js : settype() function

settype() function

The settype() function sets the type of a variable.

Version:

1103.1210

Syntax:

settype(var_name, var_type)

Parameters:

Name Description Required /
Optional
Type
var_name The variable being converted. Required Mixed*
var_type Type of the variable. Possible values are : boolean, integer, float, string, array, object , null. Optional String

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Example of php.js settype() function

In the following web document, settype() function set the types of different variables.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>php.js : settype() function example</title>
<script type="text/javascript" src="../../phpjs/variable/variable.min.js"> </script>
</head>
<body>
<h1 style="color: green">php.js : settype() function example</h1>
<h3>Set the type of a variable...</h3>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
var1='198';
var2='031';
settype('var1', 'integer');
settype('var2', 'integer');
document.write(var1+'<br />');
document.write(var2+'<br />');
document.write(var1+var2);
//]]>
</script>
</body>
</html>

Output of the function:

198
31
229 

View example of php.js settype() function in browser

Download phpjs.zip

Previous: php.js : serialize() function
Next: php.js : strval() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/phpjs/variable/settype.php