This question already has answers here:
In PHP: How to call a $variable inside one function that was defined previously inside another function?
(2 answers)
Closed 5 years ago.
For instance I have the following class
class myClassName{
function funtionOne(){
$a = '123';
$b = '456';
$c = '789';
$d = 'var_value';
$e = 'var_value';
$f = 'var_value';
}
function funtionTwo(){
$a = '123';
$b = '456';
$c = '789';
$g = 'var_value';
$h = 'var_value';
$i = 'var_value';
}
}
As you can see the variables $a, $b, $c are in both methods. My question is: How can I extend a method so get something like this:
class myClassName{
function funtionABC(){
$a = '123';
$b = '456';
$c = '789';
}
function funtionOne(){
include functionABC's content here
$d = 'var_value';
$e = 'var_value';
$f = 'var_value';
}
function funtionOne(){
include functionABC's content here
$g = 'var_value';
$h = 'var_value';
$i = 'var_value';
}
}
Note: I do not want to return anything from the function, just want to retrieve the content, as you would with an include() or a trait. Thank you.
class myClassName{
public $a, $b, $c;
function Init_funtionABC(){
$this->a = '123';
$this->b = '456';
$this->c = '789';
}
function funtionOne(){
Init_funtionABC();
//you can access $this->a , $this->b , $this->c here
$d = 'var_value';
$e = 'var_value';
$f = 'var_value';
}
function funtionOne(){
Init_funtionABC();
//you can access $this->a , $this->b , $this->c here
$g = 'var_value';
$h = 'var_value';
$i = 'var_value';
}
}
Related
$a = 0;
function a(){
global $a;
$success = b();
if ($success) {
return $a;
$a = 0;
}
}
function b(){
global $a;
$a += 9;
return true;
}
In function a if function b is success then i want to return $a and then set $a to 0
how can i do this ?
You could try creating a temporary variable and then set $a before returning.
$temp = $a;
$a = 0;
Return $temp;
this is a very simple program, but when i run it, it just out put the first echo statement, which is 3, others are not displaying. i declared it global in the function, why it dose not work, strangely, when i was coding another program declaring a global array variable in a function it works perfectly, please explain it in detail, thanks
$a = 1;
$b = 2;
$c = $a + $b;
echo $c ."<br>";
function aaa()
{
global $a;
global $b;
$d = $a + $b;
echo $a ."<br>";
function ccc()
{
global $d;
$e = $c + 1;
echo $e;
}
}
The problem with your code is that you've put the function ccc inside the funciton aaa and that is not the correct way of doing what you're trying to do.
The correct way would be to create a Class aaa and then declaring ccc as its method.
$a = 1;
$b = 2;
$c = $a + $b;
$d = 0;
function aaa() {
global $a;
global $b;
global $d;
$d = $a + $b;
echo $a ."<br>";
}
function ccc() {
global $c;
global $d;
$e = $c + 1;
echo $e;
}
echo $c ."<br>";
aaa();
ccc();
public function Encrypt($message)
{
$character = str_split($message);
$encrypted = '';
foreach ($character as $character)
{
$encrypted .= (ord($character). '.');
}
return $encrypted;
}
I use that code to generate ASCII numbers. Example of the result that I generated
$a = 1.2.4.3.4.3
$b = 1.4.3.2.4.3
Then I want both together (1+1,2+4,4+3,3+2,4+4,3+3) then the result is
$c = 2.6.7.5.8.6
Is it possible to do that ? Can anyone help me please.
It's definitely possible:
$a = '1.2.4.3.4.3';
$b = '1.4.3.2.4.3';
$result = join('.', array_map(
function($a, $b) { return $a + $b; },
explode('.', $a),
explode('.', $b)
));
var_dump($result);
Explanation:
split by .
summarize
join back
Ideone: http://ideone.com/uzBVed
Perhaps you could use a function like this?
function add_number_strings($a, $b) {
$a_arr = explode('.', $a);
$b_arr = explode('.', $b);
$c_arr = array();
for ($i=0; $i<count($a_arr); $i++) {
$c_arr[] = $a_arr[$i] + $b_arr[$i];
}
return implode('.', $c_arr);
}
// Testing
$a = '1.12.9.4.3.2.1';
$b = '2.3.2.4.3.2.1';
$c = add_number_strings($a, $b);
var_dump($c); // should be 3.15.11.8.6.4.2
I'm creating a function which passing parameter by reference. I just can't get return value with this code and I need your help.
function wow ($id, &$a, &$b)
{
$detail[0][0] = 1;
$detail[0][1] = 2;
$detail[0][2] = 3;
$detail[0][3] = 4;
$detail[1][0] = -1;
$detail[1][1] = -2;
$detail[1][2] = -3;
$detail[1][3] = -4;
for($i=0; $i<=$id; $i++)
if ($detail[$i][3] == 4)
{
$a = $detail[$i][0];
$a = $detail[$i][1];
$a = $detail[$i][2];
$a = $detail[$i][3];
}
else
{
$b = $detail[$i][0];
$b = $detail[$i][1];
$b = $detail[$i][2];
$b = $detail[$i][3];
}
}
This is the way I call the function.
$a = $b = null;
wow(1, $a, $b);
echo $a[0]." ".$a[1]." ".$a[2]." ".$a[3]." ".$a[4]." ".$b[0]." ".$b[1]." ".$b[2]." ".$b[3]." ".$b[4]." ";
I think you are missing how arrays work:
for($i=0; $i<=$id; $i++)
if ($detail[$i][3] == 4)
{
$a = $detail[$i][0];
$a = $detail[$i][1];
$a = $detail[$i][2];
$a = $detail[$i][3];
}
Each of these statemenets is overwriting the previous one.
Try something like this:
for($i=0; $i<=$id; $i++)
if ($detail[$i][3] == 4)
{
$a = $detail[$i];
}
That was you are copying the entire array inside the first one into $a.
Additionally, you set both $a and $b to null, yet only set one or the other in your function - you will always return at least a warning telling you about a null variable you are trying to echo.
Consider the following code:
class A {
private $a;
function f() {
A::a = 0; //error
$this->a = 0; //error
$self::a = 0; //error
}
}
How can I change $a in f()?
You were close. Either:
self::$a = 0; //or
A::$a = 0;
If it's static, or:
$this->a = 0;
If it's not.
I believe the syntax is:
self::$a
obviously we all have been tricked by the title of the question, though here is how you change $a's value.
<?php
class A {
private $a;
function f() {
//A::a = 0; //error
$this->a = 10; //ok
//$self::a = 0; //error
}
function display() {
echo 'a : ' . $this->a . '<br>';
}
}
$a = new A();
$a->f();
$a->display();
?>
Output:
a : 10
if you want $a to be static use the following:
<?php
class A {
private static $a;
function f() {
//A::$a = 0; //ok
//$this->a = 10; //Strict Standards warning: conflicting the $a with the static $a property
self::$a = 0; //ok
}
function display() {
echo 'a : ' . self::$a . '<br>';
}
}
$a = new A();
$a->f();
$a->display();
// notice that you can't use A::$a = 10; here since $a is private
?>