I have a problem with a piece of code. This code works fine. No problem.
include ("CCheckMail.php");
$checkmail = new CCheckMail ();
$emails = array ("korkula#iservicesmail.com");
foreach ($emails as $email)
{
if ($checkmail->execute ($email)) { return 1; } else { return 0; }
}// end foreach
And now, I want to change to instead use array, I want to send the email to check by var, like this:
include ("CCheckMail.php");
funtion_name($email){
$checkmail = new CCheckMail ();
if ($checkmail->execute ($email)) { return 1; } else { return 0; }
}
} //end function
The new code doesnt work I dont understand why.
Can anyone help me?
Thanks!
Try declaring the object once and reusing it by passing it into the function as well as the email address you're checking.
//Call the checkMail class
include("CCheckMail.php");
//Define the object to work with
$checkmail = new CCheckMail();
//Function to send the emails and return response
function checkEmailFunc($email, $emailObject){
if ($emailObject->execute ($email)) { return 1; } else { return 0; }
}
//Test it out
$emails = array("korkula#iservicesmail.com", "test#example.com");
foreach ($emails as $email) {
if (checkEmailFunc($email, $checkmail)) {
echo "Success! Checked " . $email . "<br/>";
} else {
echo "Failed to check " . $email . "<br/>";
}
}
Related
I', trying to update a row on parse using PHP. I'm using this function:
if (isset($_GET['updateHistory']))
{
updateHistory($_GET['updateHistory']);
}
if (isset($_GET['yesNo']))
{
yesNo($_GET['yesNo']);
}
function updateHistory($obId,$yesNo) {
$bool = "";
if ($yesNo == "YES") {
$bool = true;
} else {
$bool = false;
}
$query = new ParseQuery("TestObject");
try {
$history = $query->get($obId);
$history->set("isHistory", $bool);
$history->save();
} catch (ParseException $ex) {
echo "Error Updating History";
}
reload();
}
The problem now is I can't pass the 2nd variable which is $yesNo using
<a href='?updateHistory=$obId&yesNo=YES'>YES</a>
How can I pass the 2nd variable? thanks!
try
if (isset($_GET['updateHistory'], $_GET['yesNo'])) {
// you should sanitize your $_GET values before using them
updateHistory($_GET['updateHistory'], $_GET['yesNo']);
}
Since your function depends on both variables being set, combine the if-statement to check both fields and do a single call to your function:
if (isset($_GET['updateHistory']) && isset($_GET['yesNo'])) {
updateHistory($_GET['updateHistory'], $_GET['yesNo']);
}
You can then drop this part altogether:
if (isset($_GET['yesNo']))
{
yesNo($_GET['yesNo']);
}
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.
I'm trying to parse PHP source code with the token_get_all(). So far everything worked out with that function, but now i need a way to get the return values of methods.
Identifying where a return is done isn't the problem. I just see no way of getting the piece of code that comes after the return value.
For example for this piece of code:
<?php
class Bla {
public function Test1()
{
$t = true;
if($t) {
return 1;
}
return 0;
}
public function Test2()
{
echo "bbb";
return; // nothing is returned
}
public function Test3()
{
echo "ccc";
$someval1 = 1;
$someval2 = 2;
return ($someval + $otherval)*2;
}
}
?>
I'm using get_token_all() to identify where a return is done:
$newStr = '';
$returnToken = T_RETURN;
$tokens = token_get_all($source);
foreach ($tokens as $key => $token)
{
if (is_array($token))
{
if (($token[0] == $returnToken))
{
// found return, now get what is returned?
}
else
{
$token = $token[1];
}
}
$newStr .= $token;
}
I have no clue how to get the piece of code that is actually returned. That is what i want to get.
Anyone any idea how i could do this?
Perhaps this might help. Though I curious to know what you are ultimately trying to do.
$tokens = token_get_all($str);
$returnCode = '';
$returnCodes = array();
foreach ($tokens as $token) {
// If return statement start collecting code.
if (is_array($tokens) && $token['0'] == T_RETURN) {
$returnCode .= $token[1];
continue;
}
// if we started collecting code keep collecting.
if (!empty($returnCode)) {
// if we get to a semi-colon stop collecting code
if ($token === ';') {
$returnCodes[] = substr($returnCode, 6);
$returnCode = '';
} else {
$returnCode .= isset($token[1]) ? $token[1] : $token;
}
}
}
i think i need a second pair of eyes.
I have some ajax calling a php file, and it's returning json. This all works fine. I'm then alerting the data elements i return for testing purposes. In doing this i narrowed down my function is not being called.
<?php
// database functions
$response = array();
$count = 1;
// connect to db
function connect() {
$response['alert'] = 'connect ran'; // does not get alerted
}
// loop through query string
foreach ($_POST as $key => $value) {
switch ($key) {
case 'connect':
$response['alert'] = 'case ran';
if ($value == 'true') {
$response['alert'] = 'if ran'; // this is what gets alerted, should be overwriten by 'connect ran'
connect(); // function call does not work?
} else {
$response['alert'] = 'false';
$mysqli->close();
}
break;
case 'otherstuff':
break;
}
++$count;
}
$response['count'] = $count;
echo json_encode($response);
?>
Any ideas? Thanks.
your $response variable is out of scope.. use global keyword inside your function to registering your outer variable(s)
function connect() {
global $response;
$response['alert'] = 'connect ran';
}
or SDC's edit:
function connect($response) {
$response['alert'] = 'connect ran';
}
connect($response);
actually you defined the result variable but in another type and you also have another result variable at the top so you put data in $result[] but you try to use $result so your code may not give you the expected result.
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.