Simple php program displaying no output or error msg - php

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;
}

Related

How can I pass a return value of a Function to another Function as Parameter?

How to pass a value returned by one function to another function.
function myFunction(){
$a = "Hello World";
return $a;
}
function anotherFunction(????){
//how can I call the return value of myFunction() as parameter in this function?
}
You got 2 choices:
save your return value in a parameter e.g.
$value = myFunction();
anotherFunction ($value);
anotherFunction ( myFunction() );
Here's how:
<?php
function myFunction() {
$a = "Hello World";
return $a;
}
function anotherFunction( $yourvariable ) {
//how can I call the return value of myFunction() as parameter in this function?
}
$myFunction = myFunction();
$anotherFunction = anotherFunction( $myFunction );
PHP code demo
<?php
function myFunction(){
$a = "Hello World";
return $a;
}
function anotherFunction($requiredParameter)
{
echo $requiredParameter; //here you will see your parameter.
}
function someOtherFunction()
{
anotherFunction(myFunction());
}
someOtherFunction();
You can use this call to pass return to another:
anotherFunction(myFunction());
And anotherFunction you need declare as below:
function anotherFunction($val) {
// your code here
}
This will pass return value of myFunction into $val parameter.
Hope this help you!

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();

Compare a function with another function

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";
}

Global variable inside class and function PHP

I'm having trouble with reading variable defined in one function in another function.
I have:
global $a;
class test{
function aa($somevar){
switch ($myvar){
case 'value':
global $a;
$a = 15;
break;
}
}
function bb(){
global $a;
echo $a;
}
}
$foo = new test();
$ccc = $foo->bb();
var_dump($ccc);
I get dump result NULL.
Thanx
At no point in your code do you assign a value to $a.
The only assignment to $a is in the test->aa method, which uses inconsistent variables and therefore even if called will never assign to $a.
You never run test->aa() to assign a value to a.
$foo = new test();
$foo->aa();
$ccc = $foo->bb();
In this case $ccc will still be null because you are echoing $a in $foo->bb() instead of returning it.
function bb() {
global $a;
return $a;
}
I would also stay away from globals and pass the variable $a on construct of the class. For example:
class test {
public $a;
function __construct($a = null) {
// pass initial var to $a if you want
$this->a = $a;
}
function aa($somevar) {
// reassign $a
$this->a = $somevar;
}
}
$foo = new test();
$foo->aa(5);
// or just $foo = new test(5);
var_dump($foo->a);
The variable $a should be a property inside the class
Here is code try this..
<?php
global $a;
class test{
function aa($somevar){
switch ($somevar){
case 'value':
global $a;
$a = 15;
break;
}
}
function bb(){
global $a;
echo $a;
return $a;
}
}
$foo = new test();
$foo->aa('value');
$ccc = $foo->bb();
var_dump($ccc);
?>
Try this :
class test
{
public $a;
function aa($somevar)
{
switch ($myvar)
{
case 'value':
$this->a = 15;
break;
}
}
function bb()
{
return $this->a;
}
}
$foo = new test();
$ccc = $foo->bb();
var_dump($ccc);
UPDATED:
<?php
class test
{
var $a;
function aa($somevar)
{
switch ($somevar)
{
case 'value':
$this->a = 15;
break;
}
}
function bb()
{
return $this->a;
}
}
$foo = new test();
$foo->aa('value');
$ccc = $foo->bb();
var_dump($ccc);
?>

PHP Namespaces global keyword in function

<?php
namespace Top
{
$a = "Robert";
$b = "Richard";
$c = "Maurice";
function get_a()
{
global $a;
return $a;
}
function get_b()
{
global $b;
return $b;
}
function get_c()
{
global $c;
return $c;
}
echo namespace\Middle\get_a();
echo namespace\Middle\Bottom\get_c();
echo namespace\get_b();
}
namespace Top\Middle
{
$a = "Dauraun";
$b = "Khalid ";
$c = "Humberto";
function get_a()
{
global $a;
return $a;
}
function get_b()
{
global $b;
return $b;
}
function get_c()
{
global $c;
return $c;
}
}
namespace Top\Middle\Bottom
{
$a = "Terry";
$b = "Jesse";
$c = "Chris";
function get_a()
{
global $a;
return $a;
}
function get_b()
{
global $b;
return $b;
}
function get_c()
{
global $c;
return $c;
}
}
?>
So in the above code snippet I am trying to display the correct variable content using a function using the global keyword with the corresponding namespace yet, the desired result is not happening. The returned variable content is that of the namespace where the echo statement is used and not from the specified namespace. The output being 'RobertMauriceRichard.' Can someone please explain? Perhaps it's a misunderstanding on my part of the 'global' keyword inside a function that is in a namespace?
Because only 4 types of code are affected by namespace: classes, interfaces, functions, constants.
So your $a, $b, $c and echo statement are available - and actually the same - across the whole file.
By the time you call namespace\Middle\get_a();, $a is still "Robert", so "Robert" is returned.
Try to put the echo group into different namespace, and you'll observe different result:
namespace Top\Middle
{
/*...*/
echo \Top\Middle\get_a();
echo \Top\Middle\Bottom\get_c();
echo \Top\get_b();
}
/* outputs "DauraunHumbertoKhalid" */
namespace Top\Middle\Bottom
{
/*...*/
echo \Top\Middle\get_a();
echo \Top\Middle\Bottom\get_c();
echo \Top\get_b();
}
/* outputs "TerryChrisJesse" */

Categories