Compare a function with another function - php

I made a function which is contain an include file,
Can I compare this function with other function ?
e.g.
in a.php
<?php
$a = "this is file";
?>
in b.php
<?php
$b = "this is file";
?>
in function.php
<?php
function a(){
include("a.php");
}
function b(){
include ("b.php");
}
/*
my question is can I compare between function a() and function b() ?
like this
*/
if(a()==b()){
echo "it's same words";
}
else
{echo "not same words";}
?>
I know there's simple way to do my case, but it's just an example,
and I want use this way to do my complicated algorithm.
Regards.
Nur Haryadi

You need to put return statements in the functions.
function a() {
include("a.php");
return $a;
}
function b() {
include("b.php");
return $b;
}
Then you can use
if (a() == b())
to see if they returned the same thing.

Think this way: When does two functions are equal? When their results with same parameters returns same values. It means your functions needs to return something.
function a() {
ob_start();
include("a.php");
return ob_get_clean();
}
function b() {
ob_start();
include("b.php");
return ob_get_clean();
}
if (strcmp(a(), b()) == 0) {
echo "it's same words";
}

Related

Is it possible to return a variable inside a include() in php function?

I have a function like this
function test()
{
include("test.php");
}
echo test();
test.php
<?php
//echo 'test1';
return 'test2';
?>
echo 'test1' works but return 'test2' not works.
for me return nothing.
Yes, it is possible, but you would also need to return that value from the function:
function test()
{
return include("test.php");
}
echo test();

Calling a function with a variable variable

I'm trying to call a function with a php variable variable. You'll see in my code in function mainFunction(). If it's not possible to do it this way, is there a better way to do it, that avoids any more code? I wish it would work this way.
<?php
$a = 1;
$b = 1;
if ( $a == $b ) {
$exampleFunction = 'exampleOne';
} else {
$exampleFunction = 'exampleTwo';
}
//----------------------------------------------
mainFunction();
function mainFunction() {
global $exampleFunction;
echo 'This is mainFunction <br>';
$$exampleFunction();//Here's where I'm stuck.
}
function exampleOne() {
echo 'This is example one <br>';
}
function exampleTwo() {
echo 'This is example two <br>';
}
?>
A way to solve this problem would be to use PHP's call_user_func function. Here is the modified code (it also removes the global variable):
Code Example
<?php
$a = 1;
$b = 1;
// I'm just using this to hold the function name,
// to get rid of the global keyword. It will be passed
// as an argument to our mainFunction()
$exampleFunction = '';
if ($a == $b) {
$exampleFunction = 'exampleOne';
} else {
$exampleFunction = 'exampleTwo';
}
//----------------------------------------------
mainFunction($exampleFunction);
function mainFunction($func) {
echo 'This is mainFunction <br>';
// Use PHP's call_user_func. We are also checking to make sure
// the function exists here.
if (function_exists($func)) {
// This will call the function.
call_user_func($func);
}
}
function exampleOne() {
echo 'This is example one <br>';
}
function exampleTwo() {
echo 'This is example two <br>';
}
Output
When I run this code, it produces the following output:
This is mainFunction
This is example two
Try like
if ( $a == $b ) {
$exampleFunction = exampleOne();
} else {
$exampleFunction = exampleTwo();
}
and your functions should return like
function exampleOne() {
return 'This is example one <br>';
}
function exampleTwo() {
return 'This is example two <br>';
}
OR if you want to call them through the variable try to replace like
function mainFunction() {
global $exampleFunction;
echo 'This is mainFunction <br>';
$exampleFunction();
}
Try with $exampleFunction(); instead of $$exampleFunction();
OR
use call_user_func($exampleFunction)
check this way :-
function mainFunction() {
global $exampleFunction;
echo 'This is mainFunction <br>';
$exampleFunction();
}
Use just $exampleFunction, without $$:
<?php
function mainFunction() {
global $exampleFunction;
echo 'This is mainFunction <br>';
$exampleFunction();
}
?>
See manual of variable functions, not variable variables.
P.S.: Also, I suggest $exampleFunction to be an argument of mailFunction, rather than use globals.

Simple php program displaying no output or error msg

This is my first program in oop php. Its very simple where i would like to add a numerical value to a variable. And program must output 2.
<?php
class MyClass
{
public $a = 1;
public function abc()
{
if ($a=1){
$a+1;
}
}
}
$obj = new MyClass;
echo $obj->abc;
?>
In addition to gview's answer:
if ($a=1){
$a+1;
}
Should be:
if ($a == 1){
$a = $a + 1;
}
The = operator is for assignment, not for comparisons.
The abc() function does not return anything. Thus you get no output. If you add:
return $a;
You'll get something in the echo.
You aren't returning your results
public function abc()
{
if ($a==1){
$a++;
}
return $a;
}
I think you forgot to return the value from abc()
public function abc()
{
if ($a=1){
$a+1;
}
return $a;
}

How do I make a variable from outside a function work in that function?

function nothing() {
echo $variableThatIWant;
}
You can put "global" before the variable you want to use, Like this :
<?php
$txt = "Hello";
function Test() {
global $txt;
echo $txt;
}
Test();
?>
OR :
you can passed it as parameter, Like this :
<?php
$txt = "Hello";
function Test($txt) {
echo $txt;
}
Test($txt);
?>
source : http://browse-tutorials.com/tutorial/php-global-variables
The better way is to pass it as an argument.
function nothing($var) {
echo $var;
}
$foo = 'foo';
nothing($foo);
The evil way, and I dont know why I'm even showing you this, is to use global.
function nothing() {
global $foo;
echo $foo;
}
$foo = 'foo';
nothing();
You have to use global.
$var = 'hello';
function myprint()
{
global $var;
echo $var;
}
You can also use a class property (or member variable) if you are inside a class:
<?php
$myClass = new MyClass();
echo $myClass->nothing();
class MyClass {
var $variableThatIWant = "something that I want";
function nothing() {
echo $this->variableThatIWant;
}
}
Codepad example
You can pass it by reference if you want to modify it inside the function without having to return it:
$a = "hello";
myFunction($a);
$a .= " !!";
echo $a; // will print : hello world !!
function myFunction(&$a) {
$a .= " world";
}
Codepad example

How to return a function by another function

I intend to create a checker function which will be used by another functions. The idea was simple: if the conditions are not fulfilled, stop the function by returning it.
This is the original code:
function awesome(){
$a = "2";
if ($a != "1"){return;}
echo "awesome";
}
It worked. Because the $a was 2, the function is returned and the word "awesome" is not appeared. However, there are several functions to check. To avoid repetition, i made it this way:
function test($var){
if ($var != "1"){return;}
}
function awesome(){
test("2");
echo "awesome";
}
function awesomeagain(){
test("3");
echo "awesome";
}
but the word "awesome"s are appeared, and the both function awesome() and awesomeagain() are not returned. How to return those two by adding test() function?
Thank you. I really appreciate your help.
Your test function has to return a value, and you have to test this value in other functions. Something like this:
function test($var) {
if ($var != "1")
return false;
else
return true;
}
so, your code should be like this:
function awesome() {
if (test("2"))
echo "awesome";
}
But, if you want to avid checks, you can throw an exception. This should look like this:
function test($var) {
if ($var != "1")
throw new Exception();
}
and in other functions use
function awesome() {
try {
test("2");
echo "yeah";
} catch ($e) {}
}
function test( $var ) {
return ( $var == "1" );
}
function awesome() {
if ( test("2") ) {
echo "awesome";
}
}
function awesomeagain(){
if ( test("3") ) {
echo "awesome";
}
}
Fikry, try to re-learn the concept of conditional, this is the basic programming skill. Without it, you will make many buggy code and functions.
to return from the function you have to use return keyword
function test($var){
if ($var == "1"){
return TRUE;
}
}
function awesome(){
if (!test("2")){
return FALSE;
}
echo "awesome";
return TRUE;
}
fYou probably shouldn't rely on another function stopping another function form running. Try this instead
function test($var){
if ($var != "1"){return false;} return true;
}
function awesome(){
if(test("2"))
echo "awesome";
}
function awesomeagain(){
if(test("3"));
echo "awesome";
}
function test($var) {
if ($var != "1"){
return "awesome";
}
// do something else when $var is not "1" here.
}
function awesome(){
echo test("2");
}
function awesomeagain(){
echo test("3");
}
That's it! PHP is awesome! :)

Categories