{"id":1221,"date":"2018-06-02T13:00:25","date_gmt":"2018-06-02T13:00:25","guid":{"rendered":"http:\/\/chewett.co.uk\/blog\/?p=1221"},"modified":"2018-06-03T23:17:52","modified_gmt":"2018-06-03T22:17:52","slug":"how-combining-php-flags-works-for-function-calls","status":"publish","type":"post","link":"https:\/\/chewett.co.uk\/blog\/1221\/how-combining-php-flags-works-for-function-calls\/","title":{"rendered":"How combining PHP flags works for function calls"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1228\" data-permalink=\"https:\/\/chewett.co.uk\/blog\/1221\/how-combining-php-flags-works-for-function-calls\/combining_php_flags\/\" data-orig-file=\"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/combining_php_flags.jpg?fit=800%2C300&amp;ssl=1\" data-orig-size=\"800,300\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"combining_php_flags\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/combining_php_flags.jpg?fit=300%2C113&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/combining_php_flags.jpg?fit=678%2C254&amp;ssl=1\" class=\"aligncenter size-full wp-image-1228\" src=\"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/combining_php_flags.jpg?resize=678%2C254\" alt=\"\" width=\"678\" height=\"254\" srcset=\"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/combining_php_flags.jpg?w=800&amp;ssl=1 800w, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/combining_php_flags.jpg?resize=300%2C113&amp;ssl=1 300w, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/combining_php_flags.jpg?resize=768%2C288&amp;ssl=1 768w, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/combining_php_flags.jpg?resize=50%2C19&amp;ssl=1 50w\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" \/><\/p>\n<p>Here I talk a bit about how combining PHP flags work and why we use a vertical bar to combine them.<\/p>\n<p><!--more--><\/p>\n<h2>What can flags do with functions?<\/h2>\n<p>Some PHP functions have a parameter called <code>$options<\/code> that allow changing how it operates. One or many flags can be passed into this field to configure the function.<\/p>\n<p>An example function is <code>json_encode<\/code> which has a number of flags, for example\u00a0<strong><code>JSON_HEX_QUOT<\/code><\/strong>,\u00a0<strong><code>JSON_HEX_TAG<\/code><\/strong>,\u00a0<strong><code>JSON_HEX_AMP<\/code><\/strong>.<strong><code><\/code><\/strong><\/p>\n<p>These can be used with the function such as<\/p>\n<pre>json_encode($data, JSON_HEX_QUOTE)<\/pre>\n<p>Or combining multiple flags:<\/p>\n<pre>json_encode($data, JSON_HEX_QUOTE | JSON_HEX_TAG)<\/pre>\n<h2>Combining Flags with the bitwise or (vertical bar |)<\/h2>\n<p>Above I have shown how we combine multiple flags using the vertical bar symbol. In the background each flag is actually a number representing a bit mask. For example three flags could be represented as:<\/p>\n<pre>FLAG_1 = 001 = 1\r\nFLAG_2 = 010 = 2\r\nFLAG_3 = 100 = 4<\/pre>\n<p>Here each flag uses a different bit to represent the flag. Using bitwise or we can combine different flags for example:<\/p>\n<pre>FLAG_1 | FLAG_2 = 011 = 3\r\nFLAG_2 | FLAG_3 = 110 = 6\r\nFLAG_1 | FLAG_3 = 101 = 5<\/pre>\n<p>In each case, each bit position declares whether the flag has been set, the first bit position (right most) represents <code>FLAG_1<\/code>, the second (middle) <code>FLAG_2<\/code>, and the third (left most) <code>FLAG_3<\/code>.<\/p>\n<p>When combining them we can use this bit pattern to check what flags are set. So we know that <code>011<\/code> means <code>FLAG_1<\/code> and <code>FLAG_2<\/code> has been set.<\/p>\n<p>By combining flags we can use a single variable to represent multiple options for a function.<\/p>\n<h2>When might using + to combine flags cause issues<\/h2>\n<p>Since the representation of the bit masks are numbers (shown above) there may be some temptation to combine them using numerical addition (plus +). In many cases this will work identically, for example:<\/p>\n<pre>FLAG_1 | FLAG_2 = 011 = 3\r\nFLAG_1 + FLAG_2 = 3 = 011<\/pre>\n<p>As an example above we can see that in the above case there is no difference.<\/p>\n<p>However, sometimes flags are created which are a combination of basic flags. We could define a new flag.<\/p>\n<pre>FLAG_12 = FLAG_1 | FLAG_2 = 011 = 3\r\nFLAG_12 = FLAG_1 + FLAG_2 = 3 = 011<\/pre>\n<p>Again for the above operations both numerical plus and bitwise or works identically. However we now can have issues\u00a0if we use a flag that is a combination of two flags and one of its parts. Below is an example to explain this better:<\/p>\n<pre>FLAG_12 | FLAG_1 = 011 = 3\r\nFLAG_12 + FLAG_1 = 4 = 100<\/pre>\n<p>In this case, numerical addition combines <code>FLAG_1<\/code> again which then increases the numerical value by one. This then changes the binary representation to <code>100<\/code> and makes it a different flag. Whereas, using bitwise or keeps the binary representation as <code>011<\/code> representing <code>FLAG_1<\/code> and <code>FLAG_2<\/code>.<\/p>\n<p>This will only become a problem if you begin to add flags together multiple times. This could occur either by duplicating the flag in the call or using combination flags. If you are careful numerical additional can be used in place of bitwise or for combing flags.<\/p>\n<p>However, using the binary or operator reduces the likelihood for confusion as all PHP documentation pages use this operator to combine flags. In addition to eliminating the risk of combining multiple of the same flags and<\/p>\n<h2>Summary<\/h2>\n<p>We can use flags to change the operation of different functions. When you want to use multiple flags you combine these with the bitwise or operation. We also learnt why numerical additional can be used to combine flags but mentioned the warnings when doing so.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here I talk a bit about how combining PHP flags work and why we use a vertical bar to combine them.<\/p>\n","protected":false},"author":1,"featured_media":1224,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Yesterday I talked about how combining #PHP flags work for function calls.","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[268,267,66],"class_list":["post-1221","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-informational","tag-bitwise-or","tag-flags","tag-php"],"wppr_data":{"cwp_meta_box_check":"No"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/combining_flags_php.jpg?fit=800%2C800&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p2toWX-jH","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1304,"url":"https:\/\/chewett.co.uk\/blog\/1304\/installing-an-rpm-on-fedora-28\/","url_meta":{"origin":1221,"position":0},"title":"Installing an RPM on Fedora 28","author":"Chewett","date":"June 30, 2018","format":false,"excerpt":"Today I detail the command line arguments needed to install an RPM on Fedora 28. Installing an RPM on Fedora 28 Once you have downloaded an RPM file you can easily install it using the following command. rpm -Uhv <the rpm file> The full details of the command flags are:\u2026","rel":"","context":"In &quot;Informational&quot;","block_context":{"text":"Informational","link":"https:\/\/chewett.co.uk\/blog\/category\/informational\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/installing_rpm_fedora.jpg?fit=800%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/installing_rpm_fedora.jpg?fit=800%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/installing_rpm_fedora.jpg?fit=800%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/06\/installing_rpm_fedora.jpg?fit=800%2C800&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":613,"url":"https:\/\/chewett.co.uk\/blog\/613\/backup-raspberry-pi-rsync\/","url_meta":{"origin":1221,"position":1},"title":"Backup a Raspberry Pi with Rsync","author":"Chewett","date":"December 20, 2017","format":false,"excerpt":"I am going to upgrade our previous Raspberry Pi backup script that previously used Secure Copy to one that uses rsync. The primary reason for this is that rsync will only copy new or changed files over. This post goes through the reasons why you might want to use rsync\u00a0instead\u2026","rel":"","context":"In &quot;Raspberry Pi Cluster&quot;","block_context":{"text":"Raspberry Pi Cluster","link":"https:\/\/chewett.co.uk\/blog\/category\/raspberry-pi-cluster\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/12\/rsync_backup_raspberry_pi.jpg?fit=800%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/12\/rsync_backup_raspberry_pi.jpg?fit=800%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/12\/rsync_backup_raspberry_pi.jpg?fit=800%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/12\/rsync_backup_raspberry_pi.jpg?fit=800%2C800&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":189,"url":"https:\/\/chewett.co.uk\/blog\/189\/why-are-phps-function-names-a-mess\/","url_meta":{"origin":1221,"position":2},"title":"Why are PHP&#8217;s function names a mess?","author":"Chewett","date":"May 22, 2015","format":"quote","excerpt":"I have always wondered this, its exceptionally hit and miss and there isnt any real standards.\u00a0I recently found the reason on the internet Well, there were other factors in play there. htmlspecialchars was a very early function. Back when PHP had less than 100 functions and the function hashing mechanism\u2026","rel":"","context":"In &quot;Informational&quot;","block_context":{"text":"Informational","link":"https:\/\/chewett.co.uk\/blog\/category\/informational\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1431,"url":"https:\/\/chewett.co.uk\/blog\/1431\/simple-d3-js-version-5-data-binding-and-updating-example-and-code\/","url_meta":{"origin":1221,"position":3},"title":"Simple D3.js version 5 data binding and updating example and code","author":"Chewett","date":"August 11, 2018","format":false,"excerpt":"This post goes through the process of binding data to elements and creating a simple updatable SVG graphic using D3.js version 5. Data binding and updating in D3.js version 5 In D3.js version 4 there was quite a big update to how data is bound to elements and updated. This\u2026","rel":"","context":"In &quot;Software&quot;","block_context":{"text":"Software","link":"https:\/\/chewett.co.uk\/blog\/category\/software\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/08\/d3_simple_data_binding.jpg?fit=800%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/08\/d3_simple_data_binding.jpg?fit=800%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/08\/d3_simple_data_binding.jpg?fit=800%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/08\/d3_simple_data_binding.jpg?fit=800%2C800&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":2734,"url":"https:\/\/chewett.co.uk\/blog\/2734\/rsync-entire-raspberry-pi-to-windows\/","url_meta":{"origin":1221,"position":4},"title":"Rsync Entire Raspberry Pi to Windows","author":"Chewett","date":"November 21, 2020","format":false,"excerpt":"This post includes a short snippet to copy your entire Raspberry Pi filesystem to Windows using Rsync. Copying and Archiving particular directories If you want to rsync specific directories rather than the entire Raspberry Pi I suggest you read my previous blog post. It details how to backup a Raspberry\u2026","rel":"","context":"In &quot;Raspberry Pi Cluster&quot;","block_context":{"text":"Raspberry Pi Cluster","link":"https:\/\/chewett.co.uk\/blog\/category\/raspberry-pi-cluster\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2020\/11\/raspberrypi_rsync_posticon_OUTPUT.png?fit=1200%2C628&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2020\/11\/raspberrypi_rsync_posticon_OUTPUT.png?fit=1200%2C628&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2020\/11\/raspberrypi_rsync_posticon_OUTPUT.png?fit=1200%2C628&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2020\/11\/raspberrypi_rsync_posticon_OUTPUT.png?fit=1200%2C628&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2020\/11\/raspberrypi_rsync_posticon_OUTPUT.png?fit=1200%2C628&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3073,"url":"https:\/\/chewett.co.uk\/blog\/3073\/celebrating-30-years-of-php-highlights-from-jetbrains-phpverse-2025\/","url_meta":{"origin":1221,"position":5},"title":"Celebrating 30 Years of PHP: Highlights from Jetbrains PHPVerse 2025","author":"Chewett","date":"July 6, 2025","format":false,"excerpt":"This year I (virtually) attended Jetbrains PHPVerse 2025 celebrating 30 years of PHP. I have written about two sessions which most interested me. 30 Years of PHP To celebrate 30 years of PHP, Jetbrains brought together a number of speakers to talk about PHP's past successes and the future of\u2026","rel":"","context":"In &quot;Informational&quot;","block_context":{"text":"Informational","link":"https:\/\/chewett.co.uk\/blog\/category\/informational\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2025\/07\/phpverse-4.webp?fit=1200%2C675&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2025\/07\/phpverse-4.webp?fit=1200%2C675&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2025\/07\/phpverse-4.webp?fit=1200%2C675&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2025\/07\/phpverse-4.webp?fit=1200%2C675&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2025\/07\/phpverse-4.webp?fit=1200%2C675&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=1221"}],"version-history":[{"count":4,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1221\/revisions"}],"predecessor-version":[{"id":1230,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1221\/revisions\/1230"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/media\/1224"}],"wp:attachment":[{"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=1221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=1221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=1221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}