my defined php function not working [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want my page to display an error message "lorem ipsum" if the input for $a (from the $_POST super global) is greater than a defined maximum value ($max1 and $max2). So this is what I did:
$max1 = 2.7432;
$max2 = 274.32;
$a = $_REQUEST['a'];
function maximum(){
if ($a>$max1 || $a>$max2){
echo "<script language=javascript> alert(\"Lorem Ipsum\");</script>";
return maximum();
}
}
then I call the function here like so:
if(($fred=="fred") && ($george=="george") {
//check validity of input first by calling maximum;
maximum();
}
This is not working! What am I doing wrong?

This is a scope issue. $a, $max1, and $max2 is not in scope inside of your function. You need to pass it as a parameter for it to be available (i.e. in scope) within your function.
Also, your function doesn't work. In a nutshell you need to return the string from your function and then capture or echo it out when calling the function.
$max1 = 2.7432;
$max2 = 274.32;
$a = $_REQUEST['a'];
function maximum($a, $max1, $max2){
if ($a>$max1 || $a>$max2){
return "<script language=javascript> alert(\"Lorem Ipsum\");</script>";
}
}
if(($fred=="fred") && ($george=="george")) {
//check validity of input first by calling maximum;
echo maximum($a, $max1, $max2);
}

Please change your function like bellow and check the comments what mistake you did
function maximum()
{
global $max1,$max2,$a;//make $a,$max1,$max2 available inside this function
if ($a > $max1 || $a > $max2) {
echo "<script language=javascript> alert(\"Lorem Ipsum\");</script>";
//return maximum(); remove this line. Otherwise itwill go infinte loop
}
}
http://php.net/manual/en/language.variables.scope.php

Related

How to assign a variable outside php function [duplicate]

This question already has answers here:
Are PHP Variables passed by value or by reference?
(16 answers)
How to declare a global variable in php?
(10 answers)
Closed 3 years ago.
I would like to be able to assign the name of a variable outside the function so that the function can assign the chosen variable. It does not seem to work. Anyone have any ideas?
Below is my attempt, however the $admin variable is empty after running the function.
function assign_check($variable, $check) {
if (empty($_POST[$check])) {
$variable = "no";
} else {
$variable = $_POST[$check];
}
}
assign_check('$admin', 'admin');
My question is not about the use of global variables.
You can request a reference in the function body.
function assign_check(&$variable, $check) {
$variable = 'hello';
}
And call passing a variable (reference).
assign_check($admin, 'admin');
$admin value is now 'hello';
Fitting that to your code would result in
function assign_check(&$variable, $check) {
$variable = empty($_POST[$check]) ? "no" : $_POST[$check];
}
assign_check($admin', 'admin');
But returning a proper value would be much cleaner code than using references. Using a ternary operator like presented above would it even simplify without need of a function at all.
A normal way to assign the result of a function to a variable name specified outside the function would be to have the function return the result and assign it directly to the variable when you call the function.
function assign_check($check) {
if (empty($_POST[$check])) {
return "no";
} else {
return $_POST[$check];
}
}
$admin = assign_check('admin');
I would do it this way unless there was a compelling reason to do it otherwise.
For the specific type of thing it looks like this function is intended to do, I would suggest looking at filter_input.

Does PHP have keyword or feature that shortens repetition like jquery $(this)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Does PHP have keyword or feature that shortens code and repetition like jquery $(this)?
For example
// How to avoid typing same variable twice
echo isset($_POST['foo']) ? $_POST['foo'] : '';
In Jquery
<script>
// Avoid typing $("#button-element") again
$("#button-element").click(function() {
$(this).css("border-color", "#000");
});
</script>
No. And that code wouldn't work in JS either.
In JS, this is -- roughly speaking -- the object that the current function was called as a method on. It is not "the last thing I mentioned". The JS equivalent of your second code:
_POST['foo'] ? this : ''
would return an object if _POST['foo'] were set. It would not return the value of _POST['foo'].
There is no variable like the one you're looking for in any language I've ever used.
of course it does! you need to construct this across a class.
It works about the same as in javascript ES6 class.
$number = 4;
$This = new demoThis($number);
$This->multiplyThis(4);
class demoThis {
private $number;
private $factor;
public $result;
public function __construct($number) {
$this->number = $number;
}
function multiplyThis($factor) {
$this->factor = $factor;
$this->result = $this->number * $this->factor;
return $this->result;
}
}
echo isset($_POST['foo']) ? $This->result : '';

Loop until variable is isset then excute a code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I try to do a while loop but something is missing I need to loop until variable is isset then execute a code something like dis :
$counter=o;
while(null!==($var)){
$counter ++;
}
if (isset($var)){
excute code ....
}
You mean like this?
$counter = 0;
while (!isset($var)) {
$counter ++;
echo $counter, PHP_EOL;
if ($counter == 10) {
$var = true;
}
}
echo 'Done', PHP_EOL;
I suggest you don't use the existance of a variable to control your logic flow - using something like a break command to kill your potentially infinite loop might be better, for example:
while (true) {
// do something
if ($someCondition) {
break;
}
}

Warning: strpos(): Empty needle in ......wordpress Plugin [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I get this error :
Warning: strpos(): Empty needle in ......popularity-contest.php on
line 2574
function akpc_is_searcher() {
global $akpc;
$referrer = parse_url($_SERVER['HTTP_REFERER']);
$searchers = explode(' ', preg_replace("\n|\r|\r\n|\n\r", ' ', $akpc->searcher_names));
foreach ($searchers as $searcher) {
if (strpos($referrer['host'], $searcher) !== false) {
return true;
}
}
return false;
}
Can someone please help me to fix this problem?
A bunch of PHP search functions use the terms "needle" and "haystack" as their parameter names, indicating what is sought and where to seek it.
The strpos function is such a function. "Empty needle" means that you have passed in a null or empty value as the needle to look for. This is like saying "search for nothing" which doesn't make sense to the function.
To fix this, check that the variable that you're passing in as the needle has an actual value. The empty function is a good choice for that.
The warning should go away if you set WP_DEBUG to false in wp_config.php. If you want to fix it, try the following:
function akpc_is_searcher() {
global $akpc;
$referrer = parse_url($_SERVER['HTTP_REFERER']);
$searchers = explode(' ', preg_replace("\n|\r|\r\n|\n\r", ' ', $akpc->searcher_names));
foreach ($searchers as $searcher) {
if ( ! empty($searcher) && strpos($referrer['host'], $searcher) !== false) {
return true;
}
}
return false;
}

Error on line that doesnt exist [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to define a link based of a $_GET variable, but it's saying there's an error on a line that doesn't exist...
<?php
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
?>
<?php
if ($ref != "") {
$link = "http://site.com/page.php?ref=$ref";
} else {
$link = "http://site.com/page.php";
}
?>
Anyone see what's up? I was pretty sure it was fine.
I've tried it multiple different ways, with isset etc... same result.
You are missing a closing }:
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
}
By the way, this code is quite redundant. empty() will also check whether the variable is set, so you don't need isset().
You can also use the ternary operator, which is for cases like this:
$ref = empty($_GET['ref']) ? null : $_GET['ref'];
And later check with:
if (!is_null($ref)) {
//whatever
}
Otherwise, in your code, when execution reaches if ($ref != "") {, the variable $ref might not even exist - this will throw an E_NOTICE, which you might not even see, depending on your settings.

Categories