The syntax of JavaScript and PHP is very similar. How similar is shown by the following function that I wrote to generate a random password:
PHP code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| <?php
function randomPassword($length){
$password = "";
for($i=0; $i<$length; $i++){
$func = rand(0,2);
if($func == 0)
$password = $password.chr(rand(48,57));
else if($func == 1)
$password = $password.chr(rand(65,90));
else if($func == 2)
$password = $password.chr(rand(97,122));
}
return $password;
}
$randomPassword = randomPassword(20);
echo($randomPassword);
?> |
<?php
function randomPassword($length){
$password = "";
for($i=0; $i<$length; $i++){
$func = rand(0,2);
if($func == 0)
$password = $password.chr(rand(48,57));
else if($func == 1)
$password = $password.chr(rand(65,90));
else if($func == 2)
$password = $password.chr(rand(97,122));
}
return $password;
}
$randomPassword = randomPassword(20);
echo($randomPassword);
?>
JavaScript code:
<script type="text/javascript" line="1">
function rand(min,max){
if (arguments.length === 0){
min = 0;
max = 32767;
}
return Math.floor(Math.random() * (max - min + 1)) + min;
}
String.prototype.chr = function(code){
return String.fromCharCode(code);
}
function randomPassword($length){
$password = "";
for($i=0; $i<$length; $i++){
$func = rand(0,2);
if($func == 0)
$password += $password.chr(rand(48,57));
else if($func == 1)
$password += $password.chr(rand(65,90));
else if($func == 2)
$password += $password.chr(rand(97,122));
}
return $password;
}
$randomPassword = randomPassword(20);
document.write($randomPassword);
</script> |
<script type="text/javascript" line="1">
function rand(min,max){
if (arguments.length === 0){
min = 0;
max = 32767;
}
return Math.floor(Math.random() * (max - min + 1)) + min;
}
String.prototype.chr = function(code){
return String.fromCharCode(code);
}
function randomPassword($length){
$password = "";
for($i=0; $i<$length; $i++){
$func = rand(0,2);
if($func == 0)
$password += $password.chr(rand(48,57));
else if($func == 1)
$password += $password.chr(rand(65,90));
else if($func == 2)
$password += $password.chr(rand(97,122));
}
return $password;
}
$randomPassword = randomPassword(20);
document.write($randomPassword);
</script>