Transform output php - php

I have this, which is calling an integer value say: 2500.
echo "number_format({$row2['count(cID))";
This gives me an undesired output.
How do I fix this? How am i implementing the function wrong here? (I mean obviously I am)

If I guessed right, you want something like this:
echo number_format($row2['count(cID)']);
I suppose you are getting a column from the database and calling it 'count(cID)', but it's really hard to know what you need. Try providing more details.

Didn't you mean
echo number_format($row2[count($cId)]);
?

Related

sprintf wrong result in some cases

I found some strange cases accidentally that are not working with sprintf php function. Anybody had the same issues or knows an explanation for it?
Here is the case that is not working:
$value = 1.15;
echo sprintf("%03d",($value*100));
If $value is defined by 1.15 * 2^n, then the result always gonna be wrong.
If I run the function with another value, it's working well.
For correction, I used strval function like this:
echo sprintf("%03d",(strval($value*100)));
This way it works without any problems.
Somebody has an ideea what it's happening?
Thank you in advance!

access php variable through concatenation

I was trying to achieve something simple and was wondering if it was possible. Google search proved unhelpful this time. Probably it's not possible, but I am not sure. I have the following code:
<?php
//Enter your code here, enjoy!
$query1="yay";
$query2="it";
$query3="works";
for($x=1;$x<=3;$x++){
$query="\$query".$x; //I need to assign the above variables
echo $query;
}
?>
I want the output to be "yayitworks" but instead I get "$query1$query2$query3". Is there any way to get my result? I know a switch statement will help me achieve this, but am just curious.
Thanks in advance...:-)
What you want is variable variables: http://php.net/manual/en/language.variables.variable.php
$query = ${"query".$x}

Get an int at the beginning of a string with PHP

Supposing I have:
$yeah='71263andsomemore632';
How may I get two variables like this:
$intAtBeginning=71263;
$theRest='andsomemore632';
I really don't know how to do this, since both integers might have a different length. I promise I will research on whatever you help me with, to understand it and sleep happy today ;)
Thanks.
An easy way would be to use sscanf
list($intAtBeginning, $theRest) = sscanf($yeah, '%d%s');
echo $intAtBeginning.'<br>'.$theRest;

PHP Code - no idea if it does something

i have the following code snippet, in an appliation working with Zend Framework. I know what Zend Date does.. thats not the problem.
But the coder simply calls "$date" , and i dont know if this does something?
$date = new Zend_Date(time());
$date->addDay(1);
$date;
// save date, or do something else
...
I can't imagine a situation where simply stating a variable, whether it be an object, string, or otherwise, would perform some kind of action. It might just be a mistake.
As far as I can tell, it does nothing except using up some CPU cycles.
Maybe it had other use before, like echo $date;. Just writing a variable does absolutely nothing.
It adds a day to $date. Where is the confusion? The 3rd line does nothing.

PHP5: Specifying a datatype in method parameters

I have a function that looks like this
class NSNode {
function insertAfter(NSNode $node) {
...
}
}
I'd like to be able to use that function to indicate that the node is being inserted at the start, therefore it is after nothing. The way I think about that is that null means "nothing", so I'd write my function call like this:
$myNode->insertAfter(null);
Except PHP throws an error saying that it was expecting an NSNode object. I'd like to stick to using the strict data typing in my function, but would like to be able to specify a null-esque value.
So, short of changing it to function insertAfter($node) { }, is there a way I can pass something else to that function?
Update: I've accepted Owen's answer because it answered the question itself. Everyone else's suggestions were really good and I'll actually be implementing them in this project, thanks!
sure, just set a default "null"
function(NSNode $node = null) {
// stuff....
}
result being:
$obj->insertAfter(); // no error
$obj->insertAfter(new NSNode); // no error
$obj->insertAfter($somevar); // error expected NSNode
No, you can't pass something else to the function as that defeats the purpose of the static typing. In this situation, something like C# nullable would be nice i.e. NSNode?
I'd suggest creating NSNode::insertFirst() although I think that you have it the wrong way round, why is a node inserting itself, shouldn't the collection be inserting and taking the node as a parameter?
Better to have a function insertAtBeginning() or insertFirst() for legibility anyway.
"The way you think of it" may not be the way the next guy thinks of it.
insertAfter(null) might mean many things.
Maybe null is a valid value and insertAfter means place it after the index that contains the value null.

Categories