Guys i have this code:
class Test {
public function __construct($valore) {
if ($valore != TRUE ) {
return false;
} else {
return true;
}
}
}
and in another page this:
$test = new Test("");
if ($test) {
echo "result is: TRUE";
} else {
echo "result is: FALSE";
}
Why is all the time true??
Sorry and thank you!
Constructors don't have return values. So if you want a to test that value you need to have a method do this for you.
class Test
{
private $valore;
public function __construct($valore) {
$this->valore = $valore;
}
public function test() {
return (bool) $valore;
}
}
$test = new Test("");
if ($test->test()) {
echo "result is: TRUE";
} else {
echo "result is: FALSE";
}
Demo
It's always true because the $test object is always NOT false, it's an object. The return value of the constructor isn't what you're testing.
class Test {
var $valore;
public function __construct($valore) {
if ($valore != TRUE ) {
$this->valore = false;
} else {
$this->valore = true;
}
}
}
$test = new Test(FALSE);
if ($test->valore === TRUE) {
echo "result is: TRUE";
} else {
echo "result is: FALSE";
}
Related
Test.php
<?php
$a = 'D:/mydomain/Slim/Lib/Table.php';
$b = '\Slim\Lib\Table';
foreach (array($a, $b) as $value)
{
if (file_exists($value))
{
echo "file_exist";
include_once($value);
new Table();
}
else if (class_exists($value))
{
echo "class_exist";
$class = new $value();
}
else
{
echo "error";
}
}
?>
and D:/mydomain/Slim/Lib/Table.php
<?php
class Table {
function hello()
{
echo "test";
}
function justTest()
{
echo "just test";
}
}
?>
When im execute test.php in browser the output result is:
file_exist
Fatal error: Cannot redeclare class Table in D:/mydomain/Slim/Lib/Table.php on line 2
if statement for class_exist is not trigger. namespace \Slim\Lib\Table is never exist.
The second, optional, parameter of class_exists is bool $autoload = true, so it tries to autoload this class. Try to change this call to class_exists( $value, false) See the manual.
The first if can be changed to:
if(!class_exists($value) && file_exists($file)
actually there are other problems:
$a = 'D:/mydomain/Slim/Lib/Table.php';
$b = 'Table'; //Since you don't have a namespace in the Table class...
//This ensures that the class and table are a pair and not checked twice
foreach (array($a=>$b) as $file=>$value)
{
if (!class_exists($value) && file_exists($file))
{
echo "file_exist";
include_once($file);
$class = new $value();
}
else if (class_exists($value))
{
echo "class_exist";
$class = new $value();
}
else
{
echo "error";
}
}
I have a code Like this
function deletes2()
{
foreach ($_POST['selector'] as $id)
{
$this->retailer_model->deletes($id);
}
}
Now i want to echo message like
echo"success"; or "error"
How can i do this??
you need to return flag to check from function like
function deletes($id) {
// your queries
if query run suceess.
return true
else
return false;
}
then check in foreach
foreach ($_POST['selector'] as $id){
$return = $this->retailer_model->deletes($id);
if($return) {
echo "success";
}
else {
echo "error";
}
}
As per assumption. here will be condition:
function deletes2() {
$status = false;
foreach ($_POST['selector'] as $id){
$this->retailer_model->deletes($id);
$status = true;
}
if($status) {
return "success";
} else {
return "error";
}
}
If your foreach loop will execute then it will return success else will return error. Hope so you are finding such condition.
cheers.
How can I echo the value returned from a function, called within another function in PHP.
For example, if I have function like this:
function doSomething($var) {
$var2 = "someVariable";
doSomethingElse($var2);
}
function doSomethingElse($var2) {
// do anotherSomething
if($anotherSomething) {
echo "the function ran";
return true;
}
else {
echo "there was an error";
return false;
}
}
I want to echo the echo from the second function inside the first. The reason is because the second function can produce a string when it fails that the first cannot.
So how would I output the returned value from the second function?
Create an array containing values that you would like to return and then return that array.
function doSomethingElse($var2) {
// do anotherSomething
if($anotherSomething) {
$response['message'] = "the function ran";
$response['success'] = TRUE;
}
else {
$response['message'] = "there was an error";
$response['success'] = FALSE;
}
return $response;
}
In your other function
$result = doSomethingElse($var2);
echo $result['message'];`
im not sure on how i am going to explain this correctly.
I wanted a function to validate a string which i figured correctly.
But i want the function to return a boolean value.
And outside a function i need to make a condition that if the function returned false, or true that will do something. Here's my code.
i am not sure if this is correct.
<?php
$string1 = 'hi';
function validatestring($myString, $str2) {
if(!empty($myString)) {
if(preg_match('/^[a-zA-Z0-9]+$/', $str2)) {
}
}
else {
return false;
}
}
if(validatestring == FALSE) {
//put some codes here
}
else {
//put some codes here
}
?>
EDIT : Now what if there are more than 1 condition inside the function?
<?php
$string1 = 'hi';
function validatestring($myString, $myString2) {
if(!empty($myString)) {
if(preg_match('/^[a-zA-Z0-9]+$/', $str2)) {
return true;
}
else {
retun false;
}
}
else {
return false;
}
}
if(validatestring($myString, $myString2) === FALSE) {
//put some codes here
}
else {
//put some codes here
}
?>
Functions need brackets and parameter. You dont have any of them.
This would be correct:
if(validatestring($myString) === false) {
//put some codes here
}
An easier and more elegant method would be this:
if(!validatestring($myString)) {
//put some codes here
}
<?php
$string1 = 'hi';
function validatestring($myString) {
if(!empty($myString)) {
return true;
}
else {
return false;
}
}
if(validatestring($string1) === FALSE) {
//put some codes here
}
else {
//put some codes here
}
?>
Sidenote, since empty() already returns false ,you could simplify by doing:
function validateString($string){
return !empty($string);
}
if(validateString($myString){
// ok
}
else {
// not ok
}
To make a check and test later:
$check = validateString($myString);
if($check){ }
There's no need to check == false or === false, the function already returns a boolean, it would be redundant.
store $string1 to $myString in the function
myString=string1
<?php
$string1 = 'hi';
function validatestring($myString) {
myString=string1;
if(!empty($myString)) {
return true;
}
else {
return false;
}
}
if(validatestring() === FALSE) {
//put some codes here
}
else {
//put some codes here
}
?>
The value is AbcDefg_123.
Here is the regex:
function checkAlphNum($alphanumeric) {
$return = false;
if((preg_match('/^[\w. \/:_-]+$/', $alphanumeric))) {
$return = true;
}
return $return;
}
Should allow a-zA-Z0-9.:_-/ and space in any order or format and does need all but at least one character.
EDIT: Sorry again, looks like var_dump() is my new best friend. I'm working with XML and it's passing the tag as well as the value.
#SilentGhost thnx for the tips.
It works for me too.
<?php
class RegexValidator
{
public function IsAlphaNumeric($alphanumeric)
{
return preg_match('/^[\w. \/:_-]+$/', $alphanumeric);
}
}
?>
and this is how I am testing it.
<?php
require_once('Classes/Utility.php');
$regexVal = new RegexValidator();
$list = array("abcd", "009aaa", "%%%%", "0000(", "aaaa7775aaa", "$$$$0099aaa", "kkdkdk", "aaaaa", "..0000", " ");
foreach($list as $value)
{
if($regexVal->IsAlphaNumeric($value))
{
echo $value . " ------>passed";
echo "<BR>";
}
else
{
echo $value . "------>failed";
echo "<br>";
}
}
?>
function checkAlphNum($alphanumeric) {
$return = false;
if((preg_match('/^[A-Za-z0-9\w. \/:_-]+$/', $alphanumeric))) {
$return = true;
}
return $return;
}
print checkAlphNum("AbcDefg_123");
Returns true.