PHP Code - no idea if it does something - php

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.

Related

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;

Transform output 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)]);
?

Is this a PHP 'bug' or am I missing something?

Does anyone know anything about this problem, I have the following code:
if (strtotime($unlockInfo->UnlockReviewDate) < time()) {
echo "<h3>Please review your details and ensure they are accurate and up to date.</h3>";
$verifState = validateUnlockCode($db_conn, $unlockInfo->UnlockCode);
}
Now as is it seems to work fine but there was a problem that I seemed to be chasing around for ages.
Essentially $unlockInfo is an object returned from a mySql query which as you can probably see is evaluated against the current time. Now the validateUnlockCode function has the ability under specific circumstances to modify the database and therefore the $unlockInfo object.
Nothing, however, should be modified until after the if statement is evaluated. but when I miss the space out from the if statement, ie.
if(strto....
this seems to cause the $verifState to be set before the if is evaluated therefore calling the validate function and modifying the database premeturely.
Is this normal? is that supposed to happen? Sorry, I'm a bit confused on this one.
The space after the if does not affect anything.
If strtotime($unlockInfo->UnlockReviewDate) fails, then it (false == 0) will surely be less than the current UNIX timestamp. That'd be the first thing I check.
What does strtotime return? What is the format of UnlockReviewDate?

Trouble comparing two PHP variables

this may seem like a stupid question, but it is stumping me nontheless. I'm sure that the answer is something small. I think it's just one of those situations where I have been looking at the code for too long.
I am trying to compare two PHP variables to see if they are the same. As you can see below, I am comparing $verification_answer with strrev(date("Ymd")) which is today's date, reversed. So today, $verification_answer would be 31700102. Every time I try to do the comparison, however, the if statement executes (as a non-match).
$verification_answer = strrev(date("Ymd"));
if($verification != $verification_answer){
$failed .= "<h2>Attention:</h2><p>The verification code is incorrect. Please try again.</p>";
}
Can anyone see the issue? Thanks!
UPDATE: $verification is from HTML user input:
$verification = mysql_escape_string($_POST['verification']);
There's nothing wrong with the if statement. Display the two values and you should see some sort of difference:
var_dump($verification);
var_dump($verification_answer);
Perhaps $verification doesn't contain what you think it does, or you misspelled it earlier and assigned to a different variable, or...
I am comparing $verification_answer with strrev(date("Ymd"))
If that's actually what you intended to do, I think you messed up the name of the variable in the first line; it should be:
$verification = strrev(date("Ymd"));
If you accidentally overwrote the value of $verification_answer and used $verification in a comparison when it's undefined, the comparison will always be false. PHP will emit a warning, but if you have them disabled you won't see it
I must have spelled verification wrong somewhere...I copy and pasted 'verification' over each existing variable name and it fixed the problem. Thanks!

What is the best way to capture data returned from a function in PHP?

I am new to programming and learning with Wordpress.
the_title(); //outputs the title of the page
I want to capture the title of the page into a string variable so I can manipulate it with strtolower and str_replace functions.
The only way I have gotten it to work is with output buffering.
ob_start();
the_title();
$result = ob_get_clean();
echo str_replace(" ","-",strtolower($result));
/*there has got to be an easier way....
i dont fully understand why */
str_replace(" ","-",strtolower(the_title()));
What am I doing wrong?
If what you really are looking for is the wp_title function, the 2nd argument it takes is a boolean on whether or not it should display it or return it. Pass it false so it will return it to the var, then you can do this:
$mytitle = wp_title(null, false);
Otherwise, your only option is to find the function you're looking for and modify the source code.
There is no easier way. Your function does not return the string, it prints it, therefore you will have to use output buffering if you want to capture the output.
It's the difference between f1() and f2() in the following example.
// Returns a string, prints nothing.
function f1() {
return "String";
}
// Prints a string, returns nothing.
function f2() {
echo "String";
}
Wordpress is a HORRIBLE app to learn how to program from. It uses these global functions that "just work" but they do very specific tasks "inside 'The Loop'". As I say, this is a horrible example of what good code should be.
Thankfully (for you) there are other functions that just return the part you're looking for. Rather than me just writing what you need, you can read a full listing here. Take care that you note down which must be within the mythical Loop and which you can use anywhere.
As it happens there are even more ways to get the title, but I was really imagining for this example you would do something like:
$this_post = get_post($post); // I *think* $post is the post ID inside the loop though I could be wrong
echo $this_post->post_title;
But as another poster (correctly) says you can use a fairly simple wp_title() function to grab the current loop title.
This brings me back to perhaps wanting to explain why learning programming from Wordpress is a bad idea. They have so many damned way of doing the same damned thing that it's almost impossible to keep on top of things.
A blog is a really simple set of data (even moreso in WP's case because it isn't fully normalised) but rather than just having one way to output a title <?php echo $post->title; ?> you have umpteen ways, all doing subtly different things.
If you really want to learn how to program (instead of hacking your way around the crap that is the WP internals), creating a simple blog engine is fairly quick and fun... It's certainly how a lot of people get into a new language or framework.
And if you really want to have fun, have a look at Django.
Enough of the Wordpress rant. If you're fighting something like this in the future that doesn't have 100 ways of doing it, I really wouldn't recommend output-buffer-capturing. It uses up a whole buttload of resources for something relatively simple.
The easiest way can be as simple as just taking the source for the original function, sticking it in a new function and replacing the echo with return.
Just note there may be some database connectivity to handle that returning prematurely may break... So if the echo isn't the last statement, instead of returning right there, store the string as a variable and return at the end of the function.
just figured Id share my final solution with you guys.
This was to give my body tags unique id's in wordpress.*/
$title =wp_title(null,false);
echo strtolower(str_replace(' ','-',ltrim($title)));
//without the ltrim() 2 dashes are created before the title.
Almost every 'the_*' function in Wordpress has a 'get_the_*' counterpart. So, you just have to use
echo str_replace(" ","-",get_the_title());
And it's going to work like a charm. there's also get_the_excerpt(), get_the_content() and the_permalink() which somehow breaks the naming convention (God knows how many times I've written "get_the_permalink()" and got frustrated on why it didn't work)
Cheers!

Categories