How to convert the ASP code snippet to PHP? - php

function isChongHao(ary,i,j)
if ary(i-1,j)<>0 or ary(i+1,j) then
isChongHao=true
exit function
end if
isChongHao=false
end function

function isChongHao($ary, $i, $j) {
if($ary[$i-1][$j] != 0 || $ary[$i+1][$j]) {
return true;
}
return false;
}
--I suppose $ary contains a name of a function.--
Ooops, strike that: Joel thanks, I totally missed the fact that it was vbscript!!! O.o
Now maybe it is better...

function isChongHao($ary, $i, $j)
{
return ($ary[$i-1][$j] || $ary[$i+1][$j]);
}
You should probably check to ensure those indexes are set in the array beforehand, though, with an isset() call.

Assuming ary is an array
function isChongHao($ary,$i,$j) {
return (($ary[$i-1][$j] || $ary[$i+1][$j]) ? true : false);
}
Assuming ary is a function
function isChongHao($ary,$i,$j) {
return (($ary($i-1,$j) || $ary($i+1,$j)) ? true : false);
}

Related

Confused about purpose of this PHP function

This was on a PHP test and I really have no idea the purpose of this function:
function myFunction (int x)
{
if ((x & 1) == 0) {
return true;
} else {
return false;
}
}
Any help explaining this would be lovely
It just checks if the value is an EVEN number or not.
e.g.
myFunction(3); //this would be false
myFunction(4); //this would be true

Function to check on variables that may or may not exist - PHP

I have a function that is meant to check if two variables match, but with different values. It's a kinda complicated idea... but here's an example of its usage:
match($set1->test,"YES",$set2->test,"ON")
It will return true if $set1->test == "YES" && $set2->test == "ON"
Here's an example of how its implemented:
function match($field1,$val1,$field2,$val2) {
if ((isset($field1) && $field1 == $val1) && (isset($field2) && $field2 == $val2))
{
return true;
}
return false;
}
So the big issue here is you CANNOT do isset inside a function with the function's arguments. It's pointless, because the error gets thrown that $set1->test does not exist when the function is called, and if it isn't an object property then the variable gets initialized in the function scope anyway. It seems that the only way to get around this is to do the isset test on $set1->test and $set2->test before passing them to the function, but I really don't want to. It feels unnecessary.
My question is how can I call match($set1->test,"YES",$set2->test,"ON") when $set1->test or $set2->test has not been set?
ANSWER
I'm going to use a variation on Tamás's answer. I will have a separate function called prop, like this:
function prop($obj, $property) {
if (property_exists($obj,$property)) {
return $obj->$property;
}
return null;
}
Then I'll call match like this:
match(prop($set1,'test'),"YES",prop($set2,'test'),"ON")
Thanks!
Try using property_exists
function match($obj1, $obj2,$property,$val1,$val2) {
if ((property_exists($obj1, $property) && $obj1->$property == $val1) && (property_exists($obj2, $property) && $obj2->$property == $val2))
{
return true;
}
return false;
}
It's not very popular, but this could be a valid use for the # modifier to disable errors:
#match($set1->test, "YES", $set2->test, "ON");
I would do something like this:
function match($field1=NULL,$val1=NULL,$field2=NULL,$val2=NULL) {
$result = false;
if (!is_null($field) && !is_null($val1) && !is_null($field2) && !is_null($val2)) {
if ($field1 == $val1 && $field2 == $val2) {
$result = true;
}
else {
$result = false;
}
}
return $result;
}

Converting a PHP if/else statement to use ternary format

I want to convert this if/else statement to ternary format:
function session_active()
{
if ($_SESSION['p_logged_in']) {
return true;
}
else {
return false;
};
}
I tried:
function session_active()
{
($_SESSION['p_logged_in'] ? true : false);
}
but it always returns false.
I am looking at the examples at http://davidwalsh.name/php-ternary-examples and this seems correct as far as I can see from the examples. Why does it always return false?
You may try to simple return the $_SESSION['p_logged_in'] value :-
function session_active()
{
return (bool)$_SESSION['p_logged_in'];
}
php isnt ruby, you have to return that value from the ternary.
to elaborate in more detail...
function session_active()
{
return ($_SESSION['p_logged_in'] ? true : false);
}
Try this:
function session_active() {
return (isset($_SESSION['p_logged_in'])) ? true : false;
}
to correct your logic add return in front of your statment
to simplify it do: return (bool)$_SESSION['p_logged_in'];
There is a difference between $_SESSION['p_logged_in'] === true vs $_SESSION['p_logged_in'] != null, by returning $_SESSION['p_logged_in'] could in affect be more than what it is testing for.

why does my function always return false?

why does my function always return false?
i think the problem is caused by the isset function but i really dont know how to fix it
$big = array(
2,3,5,7,11,13,17,19,23
,29,31,37);
$fbig = array_flip ($big);
function isprime($n){
if($n < 2){
return FALSE;
}
if($n > 2147483647){
return FALSE;
}
if($n < 46341){
if(isset($fbig[$n])){
return TRUE;
} else {
return FALSE;
}
}
}
$b = 11;
if(isprime($b)){echo "lol";}
if(isset($fbig[$n])){
This line is the problem.
What you want to check is not isset($fbig[$n]) (which checks if there is something in the array at the index $n) but in_array($n, $fbig) (which checks if the array $fbig contains the value $n).
The array $fbig is not in the scope of the function since it's defined outside. But you can pass it:
if(isprime($b, $fbig)){echo "lol";}
should work just fine.
because your looking for a key, not a value
$fbig[11] is not set
you'll want to use in_array()
in this case, there are 11 items, but they are numbered from 0-10, no 11
plus, like Sarfraz said, it needs to be global
It's because your function doesn't know what $fbig is. A quick fix would be to change your function to look like this:
function isprime($n){
global $fbig;
if($n < 2){
return FALSE;
}
if($n > 2147483647){
return FALSE;
}
if($n < 46341){
return isset($fbig[$n]); // Nit picking fix!
}
}

PHP what is wrong with the syntax of this code?

I'm building some simple validation rules in php and my IDE (phped) is complaining about the syntax.
Can anyone tell me what is wrong with the following?
function notBlank($str) {
(strlen($str) == 0) ? return false : return true;
}
phped complains of 'unexpected return'
Any advice appreciated.
Thanks.
write it like this:
function notBlank($str){
return strlen($str) != 0;
}
Write it like this:
function notBlank($str) {
return ( strlen($str) == 0 ? false : true );
}
You cant use return within ternary operators. If you want to keep that syntax you have to do something like this:
function notBlank($str = '') {
$var = (strlen($str) == 0) ? false : true;
return $var;
}
Nevertheless do notice that the default way of doing things is more legible:
function notBlank($str = '') {
if(strlen($str) == 0)
return false;
else
return true;
}
Hope it helps!
GSto's answer seems the best here, though you might also like to check out php's empty function:
http://www.php.net/empty
strlen() returns 0 when the string is empty, and in PHP 0==false. So really, it's unnecessary to wrap strlen() in a function. If you want to insist on a boolean answer then cast it. ie:
(bool) strlen($string);
So instead of your function, which is assumably called in an if block, you'd just have
if(strlen($string)) //etc.

Categories