Your first example with a separate variable is actually slower. In both cases you have the $post
variable already in your scope, and reading from that doesn’t cost anything. But if you create a copy and assign that to a new variable, you are using more resources. Just reading an existing variable doesn’t cost any time.
In real life, it doesn’t matter. The difference is so small that you probably can’t even see it in performance tests unless you do that with thousands of different variables at the same request.
What you should go for is readability. Using $post->ID
makes it a bit easier to see where that value is coming from than with $post_id
. In an IDE you can click on the $post
part of $post->ID
and get some information about this object, like the other variables and maybe available methods.
Related Posts:
- Nginx FastCGI_Cache Vs PHP Caching
- Conditionally loading JavaScript based on the Advanced Custom Fields in the post
- Extremely slow pageload for long post when using the_content?
- Performance issues with large website [closed]
- Why does running get_the_excerpt() when generating JSON file take 28 seconds vs. 599 milliseconds without it?
- PHP 5.5, w3 Total Cache, and opcache [closed]
- Using get_bloginfo(‘template_directory’) or variable – performance issue
- Looking for most performant way to execute several similar WP queries within shortcodes
- WordPress Script stops suddenly
- is there a benefit in using a constant over get_stylesheet_directory_uri?
- Page load timer?
- Can/should we delete wordpress cron jobs with no action?
- How do you measure the PHP generation time of your WordPress pages?
- Huge time to first byte on live site
- Serve cookie free URLs in WordPress without using a CDN
- (Xdebug Profiler) Why are there two cachegrind files generated for one page?
- Create page in templates for pages or the pages manager?
- Perfmon performance counters on WAMP
- WordPress article with 50 000 words loading slow – 100% cpu
- In WordPress how do you remove render-blocking CSS manually without a plugin?
- Will php code slow down site?
- “Object-cache.php” disables wp_cron and even disables my entire site, and keeps reappearing by itself again after I delete it
- php include not working in custom page
- How to modify the default feed via a function
- How do I display offsite database info on my wordpress site?
- Styling the second sidebar
- first instantiated SimplePie object cosumes 5mb?
- Adding a line of text to php code
- If ‘editor’ is empty, then
- Different Subdomain changes page content
- Theme not calling Jquery properly
- customize wordpress database error page
- Removing the comment function within wordpress
- Creating Database Table vs. Adding MetaData to Post & User
- Post’s ID pattern?
- Countdown to date function?
- Include text in echoed hyperlink
- How to put a form with php code into a variable or shortcode?
- Utilize Function in Child Theme / Template File
- Add class to DIV depending on page loaded
- Need oop for wordpress theme? [duplicate]
- Display Youtube Time Automate from Key
- Content filter won’t work
- How to retrive the text : No movie by this actor
- Make a database query using WordPress
- Is the 404 page automatically displayed if a loop returns nothing?
- update_post_meta only updating on last loop of foreach
- How to have different header.php based on the user logged in?
- How do I remove a category from a wordpress loop>
- How can i count the post added in relationship field, theme display
- Link blogname and blogdescription to the Options Framework
- Liking a facebook page from wordpress website?
- Isotope Drop Down Categories Filter Function
- First Byte Time (back-end processing)
- How do I define a lookup table that will work across all PHP elements of the template?
- How to keep track of when a vote is cast?
- trouble with page_id & highlighting current page
- Convert the_permalink() function output into string
- Making my first wordpress template
- How to use array in function to get only value I want
- Apply class to every third list item? [duplicate]
- Only show image from custom field when present
- Cinema site, retrive the movies that actor play in
- How to include custom code on a page that’s set to act as homepage?
- retain querystring values when savincustom options in admin
- PHP if statement works in template files, but not header
- How Can I Edit the Registration Code?
- Error while setting role
- Editing a PHP file to include a small text piece on every page
- Get categories names as an array to use it in theme settings
- Should i use the wordpress Options table or to create database table..?
- variable value disappears in a second loop
- Force Users To Relogin
- unknown issue in ‘header.php’ preventing theme from displaying fully
- Hide a div to subscribers(user role) in one page (is_page)
- Security updates to 3.3.2
- Where is this function’s callback getting its arguments from?
- How can I use a sliced image As banner? [closed]
- persist a variable set in header.php all the way down to footer.php
- Echo title attribute php
- Can a page contain php code?
- Last class on last headline?
- Unable to Find Space in Custom Function
- Is it possible to display a DIV to not logged in users and then change it for users who are logged in?
- Strip Paragraph Tag
- Unable to process php via shortcode
- working with term_relationships table
- Creating a blog on my companies new website using WordPress but I have sidebar issues
- Creating a mobile site by adding a new class to the body tag from functions.php
- WordPress plug-in for dynamic download link?
- Using WP functions such as the_title() in an included php file
- GET problem in WordPress
- WordPress gallery shortag
- Almost Done… Post Format Code
- wordpress admin plugin menu custom css
- Help with page nav?
- Show recent posts starting at a specific number archive
- Add title=”” to A PHP Code
- Get Page ID from Backend
- Help with WordPress function inside a shortcode