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.
Related
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";
}
If I'm trying to execute the code below.
I get errors when the user isn't online on TS.
but I want it to return false is this possible somehow?
code:
$client = $ts3_VirtualServer->clientGetByName($input);
if ($client == true) {
$TSonline = "true";
}
else {
$TSonline = "false";
}
}
echo $TSonline;
Try something like this..
try {
$client = $ts3_VirtualServer->clientGetByName($input);
if ($client){
$tsOnline = true;
} else {
$tsOnline = false;
}
} catch (Exception $e) { // or whatever exception is available...
$tsOnline = false;
}
if ($tsOnline){
/* do your stuff */
}
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.