PHP question mark expression usage

Source: Internet
Author: User
The existing variable $ time may be of the time type and may be a Unix timestamp, that is, $ time & #039; 2011-12-2500: 11: 20 & #039 ;; or $ time1324742400. now I want to make a unified conversion, that is, if it is a time type, convert it to U using the strtotime ($ time) function... the existing variable $ time may be of the time type and may be a Unix timestamp, that is $time = '2011-12-25 00:11:20';Or $time = 1324742400.

Now I want to make a transformation for unification, that is, if it isTime TypeAnd use it.strtotime($time)Converts a function to a Unix timestamp. If this function is not usedstrtotime($time)Will returnfalse.

Then I use the question mark expression.$time = strtotime($time) ? strtotime($time) : $time;Is there a more concise form to meet this requirement? Do you always feel that someone else did not write this?

Reply content:

The existing variable $ time may be of the time type and may be a Unix timestamp, that is$time = '2011-12-25 00:11:20';Or$time = 1324742400.

Now I want to make a transformation for unification, that is, if it isTime TypeAnd use it.strtotime($time)Converts a function to a Unix timestamp. If this function is not usedstrtotime($time)Will returnfalse.

Then I use the question mark expression.$time = strtotime($time) ? strtotime($time) : $time;Is there a more concise form to meet this requirement? Do you always feel that someone else did not write this?

The main reason for others to write less is probably that if you writetimeIf it is in the time format, it will be executed twice.strtotimeFunction.
You can use temporary variables to solve this problem.

$temp = strtotime($time);$time = $temp ? $temp : $time;

For PHP 5.3 or above, you can also use

$time = strtotime($time) ?: $time;

I'm not sure about your problem. If it is a string timestamp, the format must beY-m-d H:i:sFormat?

$time = is_numeric($time) ? $time : strtotime($time);

In fact, you do not need to use the Trielement operator.

!is_numberic($time) && $time = strtotime($time); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.