I have a question about php (wordpress) I have a plugin(buddypress) with function bp_the_profile_field_name();
when i use
echo bp_the_profile_field_name();
It's return "Tags"
But..
if (bp_the_profile_field_name() == "Tags"){
echo "Yes, it's working";
} else {
echo "Oh no..";
}
?>
its return "Oh no.."
When i try equal to string "Tags" dosent't match.
Why? Please help
When googling to the source of the bp_the_profile_field_name function I found this source:
function bp_the_profile_field_name() {
echo bp_get_the_profile_field_name();
}
Here we can see that the the function uses echo to show the value.
If you want to get the value to compare it with something else you'll need to use a other function which will return the value.
Below the mentioned page in the 'related' part this function is mentioned:
bp_get_the_profile_field_name
Returns the XProfile field name.
Note the _get_ insteaf off _the_
Which returns a string. Source can be found here.
So your code should become:
if (bp_get_the_profile_field_name() == "Tags"){
echo "Yes, it's working";
} else {
echo "Oh no..";
}
According to documentation (https://www.buddyboss.com/resources/reference/functions/bp_the_profile_field_name/) bp_the_profile_field_name() echoes output.
If you want to compare the output of this function you need a return, not echo.
You have 2 options:
Use bp_get_the_profile_field_name() instead of bp_the_profile_field_name() in your if statement
Edit bp_the_profile_field_name() to output the result instead of echoing it.
Related
It is me who can not deal with multiple external conditional variables correctly, not PHP.
There were several solutions offered by the Stack Overflow while I was preparing this question but none of them seem to suit my case. Sorry if I haven't made out the correct one.
My goal is to execute one piece of code or just output some text in case the external variable is empty and the other one if not empty.
Say /test.php?test should return something like The variable is empty while /test.php?test=test should return The variable is not empty: test.
I've made this code:
if(isset($_GET['test']) and $test === NULL){
echo 'The variable is empty';
}
if(isset($_GET['test']) and $test !== NULL){
echo 'The variable is not empty: '.$test;
}
which in my opinion should do the trick. But it doesn't.
It just returns the if(isset($_GET['test']) and $test === NULL) condition in both cases instead.
I understand that my code is incorrect but I have no idea how to make it working.
Any help will be highly appreciated.
$_GET always returns a string when an array key exists, it could be an empty string, but a string nonetheless. See: empty()
So your code would look something like this:
if (isset($_GET['test'])) {
if (empty($_GET['test'])) {
echo 'The variable is empty';
} else {
echo 'The variable is not empty: '.$_GET['test'];
}
} else {
echo 'The variable does not exist';
}
I am confused about return statement , why we need to use return in end of function , for example
function test($a){blah;
blahh ;
blahhh;
blahhhhh;
return;}
What the use of return here? Function automatically terminates when all the statements executed , I think there is no use of return here , but this picture from http://www.w3resource.com/php/statement/return.php make me confused
So
Can someone please explain the use of return (when we not returning any value).
It depends on what you're trying to achieve.
If you write echo in several places, your code will get confusing. In general, a function that returns a value is also more versatile, since the caller can decide whether to further manipulate that value or immediately print it.
I'd recommend to stick to the convention and use return for a function.
You should check GuardClause.
Example:
function test() {
return 10;
}
$a = test(); // $a stores the value 10
echo $a; // Prints 10
echo $a + 5; // We may want to manipulate the value returned by the function. So, it prints 15.
For further reference, check What is the difference between PHP echo and PHP return in plain English?
In that context: You don't.
return breaks out of the function, but since it is the last statement in that function, you would break out of it anyway without the statement.
return passes its argument back to the caller, but since it doesn't have an argument, there is nothing to pass.
So it does nothing.
Don't assume that every piece of code you stumble across has a purpuse. It might be left over from an earlier version of the code where something else (which gave it meaning) has been removed. It might be written by someone cargo culting. It might be placeholder for future development.
"return" is important when you plan to call this function from other codes, it helps you to:
Know if the function works correctly, or not.
Obtain values from a function.
Make sure other codes are not executed after return.
It might be useless when the code is simple as your sample, let's make it more complex.
function test($a){
if(file_exists($a)){
if(is_file($a)){
return $a." IS A FILE\n";
} else if(is_dir($a)) {
return $a." IS A DIR\n";
} else {
return $a." EXISTS, BUT I DONT KNOW WHAT IT IS\n"
}
} else {
return $a." NOT EXISTS\n";
}
return 0;
}
$filecheck = test("/abc/def.txt");
if($filecheck){
echo $filecheck;
} else {
echo "unknown error";
}
Above shows how to return a value, and do some basic handling.
It is always good to implement return in your functions so you can even specify error code for your functions for reference.
Based on your example, I'll modify slightly like below:
function test($a){
blah;
blahh;
blahhh;
blahhhhh;
return 1;
}
So I know the code is running to last line.
Otherwise the function just finishes silently.
It's useful if you want a function to return a value.
i.e.
Function FavouritePie($who) {
switch($who) {
case 'John':
return 'apple';
case 'Peter':
return 'Rhubarb';
}
}
So, considering the following:
$WhatPie = FavouritePie('John');
echo $WhatPie;
would give
apple
In a really simple form, it's useful if you want a function to return something, i.e. process it and pass something back. Without a return, you'd just be performing a function with a dead end.
Some further reading:
http://php.net/manual/en/function.return.php
http://php.net/manual/en/functions.returning-values.php
To add some further context specific to the answer, if the question boils down to "Do I need to add a return for the sake of it, at the end of a function, then the answer is no. But that doesn't mean the correct answer is always to leave out your return.
it depends what you want to do with $a.
If you echo $a within the function, that function will spit out $a as soon as it is called. If you return $a, assuming you set the calling of test to a variable (i.e. $something = $test('foo')), then you can use it later on.
I am setting a session variable in a php file as:
$_SESSION['PageFrom']="check_login_test.php";
Later on the control is transferred to another php file via a form and post. The relevant code in the 2nd file is:
session_start();
$from=trim($_SESSION['PageFrom']);
pageFrom("check_login_test.php",$from);
die('I came here');
Now pageFrom is a function that should have compared the strings and said so. The original function was simply
function oldPageFrom($page,$from)
{
$page= trim($page);
$from=trim($from);
if (strcmp($page,$from)!==0)
{
echo 'The pages are not same';
}
}
Since it did not work as expected, I tried to debug the same and output as much as I can (This changed the old function, but now reveals some things interesting) . The function is
function pageFrom($page,$from)
{
echo '<br/>$page='.$page;
echo '<br/>$from='.$from;
$m=trim($page);
$n=trim($from);
echo "<br/>TrimmedPage=$m<br/>TrimmedFrom=$n";
$k= strcmp($m,$n);
echo '$k='.$k;
if($k !==0);
{
$i=strlen($from);
$j=strlen($page);
if ($i==$j)
{
echo "<br/>The string lengths are Equal $i=$j";
die('Equal in unequal');
}
else
{
echo "<br/>The string lengths are UnEqual $i<>$j";
die('UnEqual in unequal');
}
}
echo 'Works as expected. They are equal'; die('equal');
}
Strangely I get the following as output:
$page=check_login_test.php
$from=check_login_test.php
TrimmedPage=check_login_test.php
TrimmedFrom=check_login_test.php$k=0
The string lengths are Equal 20=20Equal in unequal
So the questions are:
(1)I have trimmed the strings. They are equal and of the same length. Strcmp returns 0. Then why does it enter the loop if($k !==0) at all? Note: no luck in ($k!=0) too.
You're using the following code:
if($k !==0);
{
The semi-colon (;) terminates the if clause and the subsequent { } block won't have any effect. Remove the ; after if and it will work as it should, i.e. this code below
if($k !==0)
{
<?php
function dosomething(){
echo "do\n";
}
$temp="test".dosomething();
echo $temp;
?>
expected result:testdo
but actual result is:
do
test%
I know how to change the code to get the expected result.
But what i doubt is why the code prints result like this.
Can someone explain it?Thanks!
dosomething is echoing to the screen. Since this runs first "do\n" is printed.
dosomething also doesn't return anything so the second echo is equivalent to echo "test";
In order to use the result of the call you should return it:
function dosomething(){
return "do\n";
}
Which will behave as you expect.
To clarify. In order to work out what $temp is the function must be run first which prints out "do\n" first.
Use return.
function dosomething(){
return "do\n"; }
Use a return statement instead of echo
function dosomething(){
return "do\n";
}
I'm not sure why people are getting down voted. Their answers are right.
http://codepad.org/nagGXY99
Try this:
Use return in the calling function. Doing that you will get the string where the function is being called. So function is being replaced by string and 2 strings will then con cat.
-
Thanks
I have code below, where i need to get the return value in a variable outside a class and also its print with respective code.
http://codepad.org/mAlhYBll
and below is raw code.
<?php
class test {
public function kk() {
echo "Whats up :";
return "Hello";
}
}
$obj = new test();
$obj->kk();
$abc = $obj->kk();
?>
Now how can i get value returned from a function added an image below
You need to echo $abc. The program is printing something else since you're echoing What's up within the method, remove that.
Exactly as you've done - although you call "kk()" twice, which is not nneeded, so drop line 13.