When using a funciton for example, "get_photo_id" and I use the number 6 inside.
EXAMPLE:
<?php get_photo_id("6"); ?>
i want the function get_photo_id to get the value inside (which is 6 in this case). To be able to find the number inside and assign a variable to it.
Something like this:
function get_photo_id() {
<!--something to find the value used and assign it as $valueused -->
get_cat_id('$valueused')
echo 'cat_id'
}
I hope you get what i mean... im kind of new in php!
Function arguments are passed as follows:
function foo($argument1, $arg2)
{
var_dump($argument1, $arg2, func_get_args());
}
foo("test", 12);
results in
string(4) "test"
int(12)
array(2) { [0]=> string(4) "test" [1]=> int(12) }
Do you mean function parameters?
function foo($arg)
{
echo $arg;
}
When you call foo("6") it will echo 6. However, if you want to work with numbers, its better to call foo(6) with a number, instead of a string containing a number.
I am not clear about your question, but this might be the answer from what i understand
function get_photo_id($value) {
<!--something to find the value used and assign it as $valueused -->
get_cat_id($value);
echo 'cat_id'
}
Related
I have the following code
<?php
$foo[0] = new stdclass();
$foo[0]->foo = 'bar';
$foo[0]->foo2 = 'bar';
destroy_foo($foo);
var_dump ($foo);
function destroy_foo($foo)
{
unset($foo[0]->foo);
}
?>
The output is
array(1) { [0]=> object(stdClass)#1 (1) { ["foo2"]=> string(3) "bar" } }
I would expect $foo[0]->foo to still exist outside the function, but it doesn't. If I remove the properties and just use an array instead, it works. If I change the variable name inside the function, same problem. How can I use properties but make it work as expected?
What you see as an error is a PHP behaviour that's "working as expected": see the objects and references official guide.
It's not clear what you want to achieve with your code, but you should try to pass a clone of your object to the function.
In PHP Objects will only free their resources and trigger their __destruct method when all references are unsetted. So, to achieve your desire result, you have to
assign null insteadof unsetting it.
$foo[0]->foo = null;
so, this is weird, I have this method called treetrunk that runs up a parent child relationship and is supposed to return an array if ids as a "branchPath" - the method seems to be working just fine as a var_dump in my terminating condition shows the proper array. However if i try to call the method the returned array is "NULL" - I really dont get it..
Model method:
function treeTrunk($id){//1st id in is page id
if($id == '0'){
var_dump($this->branchPath); //this shows perfect array
return $this->branchPath; //this returns null
}
else{
$q = $this->getWhere(array('id'=>$id),null,null);
array_push($this->branchPath, $q[0]['pageParent']);
$this->treeTrunk($q[0]['pageParent']);
}
}
Calling via controller:
$d = $this->pages_mdl->treeTrunk('150');
var_dump($d); // <- this == NULL
var_dump from within method outputs "array(3) { [0]=> string(3) "148" [1]=> string(3) "146" [2]=> string(1) "0" } "
You are not returning anything in else part.
else{
$q = $this->getWhere(array('id'=>$id),null,null);
array_push($this->branchPath, $q[0]['pageParent']);
$this->treeTrunk($q[0]['pageParent']);
}
should be
else{
$q = $this->getWhere(array('id'=>$id),null,null);
array_push($this->branchPath, $q[0]['pageParent']);
return $this->treeTrunk($q[0]['pageParent']);
}
As posted in you question you are passing 150 to treeTrunk() function, so it goes to else part and gives you null result. The if part will evaluate when you pass 0 to treeTrunk() function.
That is supposed to be NULL
else{
$q = $this->getWhere(array('id'=>$id),null,null);
array_push($this->branchPath, $q[0]['pageParent']);
$this->treeTrunk($q[0]['pageParent']); // You have to return this value as well
}
When you don't return that array, what else do you expect it to send back ? add a return statement there. When you are dealing with recursion then your last call returned the value to your second last call, but did that call return it back to the original caller? so add a return there
return $this->treeTrunk($q[0]['pageParent']);
And also remove the quotation marks around integer values, not only does it look bad, it sometimes stops working as expected in PHP then.
Demo
I am new to PHP OOP and I am having problem getting arrays back.
class example
{
public $array;
public function __construct()
{
$this->array = array();
}
public function do_work()
{
$this->array[] = 'test';
}
}
$test = new example();
$test->do_work();
$test->array;
I keep getting a empty array instead of 'test'.
What am I doing wrong?
This is because you never actually call the function $test->do_work(); The constructor just creates the empty array, and then you attempt to access the property. It should be empty.
Updates
I see you updated your question. If you simply echo $test->array, it should just print Array. However, when I copy your updated code and perform a var_dump($test->array), this is the output I get:
array(1) { [0]=> string(4) "test" }
Which I believe is what you are expecting. The code that you have in your question, though, should output nothing. You are doing nothing with $test->array, the variable is being evaluated and then thrown away.
Your last statement, $test->array; doesn't actually do anything. My guess is that you are using something like echo. Your code should output the array if you use for example var_dump, see the example on codepad
sorry for asking this dumb question i will try to explain as good as i can, i have never done a php function before, i could solve this with IF but i want to do my first function
What i want to do is to see if the variable is null and if it is i want to set the variable to be "N/A"
$carplate=mysql_result($result,0,"plate");
$carbrand=mysql_result($result,0,"brand");
function set_null(variablegoeshere?){
if($WHATiWANTtonull==null){
$WHATiWANTtonull="N/A";
}else{ do nothing }
}
set_null($carplate);
set_null($carbrand);
echo "carplate = $carplate | carbrand = $carbrand";
i dont understand how i will get the function to set a variable to "N/A" or do nothing, i don't want it to echo something, just set a variable and i can echo it later whenever i want
[EDIT]: Use this function:
function set_null(&$var)
{
if($var==null)
{
$var="N/A";
}
}
I am using some XML parser to get some information from API, blah blah... :)
In one place in my script, I need to convert string to int but I'm not sure how...
Here is my object:
object(parserXMLElement)#45 (4) {
["name:private"]=>
string(7) "balance"
["data:private"]=>
object(SimpleXMLElement)#46 (1) {
[0]=>
string(12) "11426.46"
}
["children:private"]=>
NULL
["rows:private"]=>
NULL
}
I need to have this string "11426.46" stored in some var as integer.
When I echo $parsed->result->balance I get that string, but if I want to cast it as int, the result is: 1.
Please help!
Thanks a lot!
you have an object, intval of an object will always be 1(if it doesnt have a __toString() magic method defined).
you can intval SimpleXMLElement and it will return 11426, but to do that, the data member of the parserXMLElement class has to be public. you might need to define a getData() method for the parserXMLElement class or make the data member public.
You need to use intval. For example:
echo intval($parsed->result->balance);
will output the value as an integer - assuming that balance is a string.