{"id":336,"date":"2019-01-31T17:21:41","date_gmt":"2019-01-31T17:21:41","guid":{"rendered":"http:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/?page_id=336"},"modified":"2021-02-25T04:34:28","modified_gmt":"2021-02-25T04:34:28","slug":"reference","status":"publish","type":"page","link":"https:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/reference\/","title":{"rendered":"Code Syntax Quick Reference"},"content":{"rendered":"<p><h4>Getting the data type of a variable<\/h4>\r\nPHP: gettype($variable);<br>\r\nJS: typeof(variable);<\/p>\r\n\r\n<p><h4>Determining type of object<\/h4>\r\nPHP: ($variable instanceof obj)<br>\r\nJS: (variable instanceof obj)<br>\r\nJava: (variable instanceof obj)<\/p>\r\n\r\n<h2>Arrays<\/h2>\r\n<section class=\"codeRefBlock\">\r\n<h3>Creating an empty array<\/h3>\r\n<span class=\"codeRef\"><strong>PHP:<\/strong> <code>$myArray = [];<\/code> <strong>OR<\/strong> <code>$myArray = array();<\/code><\/span>\r\n<span class=\"codeRef\"><strong>JS:<\/strong> <code>var array = new Array();<\/code><\/span>\r\n<span class=\"codeRef\"><strong>Java:<\/strong> <code>int[] array = new int [5];<\/code><\/span>\r\n<\/section>\r\n\r\n<section class=\"codeRefBlock\">\r\n<h3>Finding length of an array<\/h3>\r\n<span class=\"codeRef\"><strong>PHP:<\/strong> <code>count($array);<\/code><\/span>\r\n<span class=\"codeRef\"><strong>JS:<\/strong> <code>array.length;<\/code><\/span>\r\n<span class=\"codeRef\"><strong>Java:<\/strong> <code>array.length();<\/code><\/span>\r\n<\/section>\r\n\r\n<section class=\"codeRefBlock\">\r\n<h3>Printing an array<\/h3>\r\n<span class=\"codeRef\"><strong>PHP:<\/strong> <code>echo implode(\"[enter separator here]\", $myArray);<\/code> <strong>OR<\/strong> <code>print_r($myArray);<\/code><\/span>\r\n<span class=\"codeRef\"><strong>JS:<\/strong> <code><\/code><\/span>\r\n<span class=\"codeRef\"><strong>Java:<\/strong> <code><\/code><\/span>\r\n<\/section>\r\n\r\n<section class=\"codeRefBlock\">\r\n<h3>Adding an item to end of array<\/h3>\r\n<span class=\"codeRef\"><strong>PHP:<\/strong> <code>myArray[] = $variable;<\/code> <strong>OR<\/strong> <code>array_push($myArray, [element1], [element2]);<\/code><\/span>\r\n<span class=\"codeRef\"><strong>JS:<\/strong> <code>myArray.push(value);<\/code><\/span>\r\n<span class=\"codeRef\"><strong>Java:<\/strong> <code><\/code><\/span>\r\n<\/section>\r\n\r\n<section class=\"codeRefBlock\">\r\n<h3>Removing an element from end of array<\/h3>\r\n<span class=\"codeRef\"><strong>PHP:<\/strong> <code>array_pop($myArray);<\/code><\/span>\r\n<span class=\"codeRef\"><strong>JS:<\/strong> <code><\/code><\/span>\r\n<span class=\"codeRef\"><strong>Java:<\/strong> <code><\/code><\/span>\r\n<\/section>\r\n\r\n<section class=\"codeRefBlock\">\r\n<h3>Adding an element from the beginning of an array<\/h3>\r\n<span class=\"codeRef\"><strong>PHP:<\/strong> <code>$items_removed = array_unshift($myArray, [element1], [element2]);<\/code><\/span>\r\n<span class=\"codeRef\"><strong>JS:<\/strong> <code><\/code><\/span>\r\n<span class=\"codeRef\"><strong>Java:<\/strong> <code><\/code><\/span>\r\n<\/section>\r\n\r\n<section class=\"codeRefBlock\">\r\n<h3>Removing an element from the beginning of an array<\/h3>\r\n<span class=\"codeRef\"><strong>PHP:<\/strong> <code>$item_removed = array_shift($myArray);<\/code><\/span>\r\n<span class=\"codeRef\"><strong>JS:<\/strong> <code><\/code><\/span>\r\n<span class=\"codeRef\"><strong>Java:<\/strong> <code><\/code><\/span>\r\n<\/section>\r\n\r\n<section class=\"codeRefBlock\">\r\n<h3>Removing an element from an array<\/h3>\r\n<span class=\"codeRef\"><strong>PHP:<\/strong> <code>unset($myArray[0], [$myArray[1]]);<\/code><br>To reset array keys after removal use: <code>$myArray = array_values($myArray);<\/code><\/span>\r\n<span class=\"codeRef\"><strong>JS:<\/strong> <code><\/code><\/span>\r\n<span class=\"codeRef\"><strong>Java:<\/strong> <code><\/code><\/span>\r\n<\/section>\r\n\r\n<section class=\"codeRefBlock\">\r\n<h3>Check if value is in an array<\/h3>\r\n<span class=\"codeRef\"><strong>PHP:<\/strong> <code>in_array($value, $myArray);<\/code><\/span>\r\n<span class=\"codeRef\"><strong>JS:<\/strong> <code>myArray.indexOf(value);<\/code> OR <code>myArray.includes(value);<\/code><\/span>\r\n<span class=\"codeRef\"><strong>Java:<\/strong> <code><\/code><\/span>\r\n<\/section>\r\n\r\n<section class=\"codeRefBlock\">\r\n<h3>foreach<\/h3>\r\n<span class=\"codeRef\"><strong>PHP:<\/strong> <code>foreach($myArray as $value)<\/code> OR <code>foreach($myArray as $key => $value)<\/code><\/span>\r\n<span class=\"codeRef\"><strong>JS:<\/strong> <code>myArray.forEach(value){ \/\/ function body }<\/code><\/span>\r\n<span class=\"codeRef\"><strong>Java:<\/strong> <code><\/code><\/span>\r\n<\/section>\r\n\r\n<section class=\"codeRefBlock\">\r\n<h3>Slicing an array<\/h3>\r\n<span class=\"codeRef\"><strong>PHP:<\/strong> <code>array_slice($array_name, start position, number of elements to extract(O), true\/false(O));<\/code><\/span>\r\n<span class=\"codeRef\"><strong>JS:<\/strong> <code>myArray.slice(start, end);<\/code><\/span>\r\n<span class=\"codeRef\"><strong>Java:<\/strong> <a href=\"https:\/\/www.techiedelight.com\/get-slice-primitive-array-java\/\" target=\"_blank\" rel=\"noopener noreferrer\">Click here<\/a><\/span>\r\n<\/section>\r\n\r\n<h2>Strings<\/h2>\r\n<section class=\"codeRefBlock\">\r\n<h3>Split string into an array<\/h3>\r\n<span class=\"codeRef\"><strong>PHP:<\/strong> <code><\/code><\/span>\r\n<span class=\"codeRef\"><strong>JS:<\/strong> <code>$mystring.split([separator], limit]]);<\/code><\/span>\r\n<span class=\"codeRef\"><strong>Java:<\/strong> <code><\/code><\/span>\r\n<\/section>\r\n\r\n<section class=\"codeRefBlock\">\r\n<h3>Get length of a string<\/h3>\r\n<span class=\"codeRef\"><strong>PHP:<\/strong> <code>strlen($string);<\/code><\/span>\r\n<span class=\"codeRef\"><strong>JS:<\/strong> <code>string.length;<\/code><\/span>\r\n<span class=\"codeRef\"><strong>Java:<\/strong> <code>string.length;<\/code><\/span>\r\n<\/section>\r\n\r\n<p><h4>Find location of an item in a string<\/h4>\r\nPHP: strpos($str, item);<br>\r\nJS: string.indexOf(item);<br>\r\nJava: string.indexOf(item);<\/p>\r\n\r\n<p><h4>Find character at location in string<\/h4>\r\nPHP: no eqiuvalent<br>\r\nJS: string.charAt(pos);<br>\r\nJava: string.charAt(pos);<\/p>\r\n\r\n<p><h4>Returning part of or slicing a string<\/h4>\r\nPHP: substr($str, start, length);<br>\r\nJS: string.slice(start, end);<br>\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string.substring(start, end);<br>\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string.substr(start, length);<br>\r\nJava: string.substring(start, end);<\/p>\r\n\r\n<p><h4>Trim characters in a string<\/h4>\r\nPHP: trim($str, characterList);<br>\r\nJS: string.trim(); &#8211; <b>removes whitespace only<\/b><br>\r\nJava: string.trim(); &#8211; <b>removes whitespace only<\/b<\/p>\r\n\r\n","protected":false},"excerpt":{"rendered":"Getting the data type of a variable PHP: gettype($variable); JS: typeof(variable); Determining type of object PHP: ($variable instanceof obj) JS: (variable instanceof obj) Java: (variable instanceof obj) Arrays Creating an empty array PHP: $myArray = []; OR $myArray = array(); JS: var array = new Array(); Java: int[] array = new int [5]; Finding length [&hellip;]","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"spay_email":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.1.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Code Syntax Quick Reference - Web Development Reference<\/title>\n<meta name=\"description\" content=\"Code Syntax Quick Reference - Web Development Reference\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Code Syntax Quick Reference - Web Development Reference\" \/>\n<meta property=\"og:description\" content=\"Code Syntax Quick Reference - Web Development Reference\" \/>\n<meta property=\"og:url\" content=\"http:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/reference\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Development Reference\" \/>\n<meta property=\"article:modified_time\" content=\"2021-02-25T04:34:28+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"2 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/#website\",\"url\":\"http:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/\",\"name\":\"Web Development Reference\",\"description\":\"Web Development Solutions Explained -- Easy\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"http:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/reference\/#webpage\",\"url\":\"http:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/reference\/\",\"name\":\"Code Syntax Quick Reference - Web Development Reference\",\"isPartOf\":{\"@id\":\"http:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/#website\"},\"datePublished\":\"2019-01-31T17:21:41+00:00\",\"dateModified\":\"2021-02-25T04:34:28+00:00\",\"description\":\"Code Syntax Quick Reference - Web Development Reference\",\"breadcrumb\":{\"@id\":\"http:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/reference\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/reference\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/reference\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"http:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/\",\"url\":\"http:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"http:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/reference\/\",\"url\":\"http:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/reference\/\",\"name\":\"Code Syntax Quick Reference\"}}]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/P7o7aF-5q","_links":{"self":[{"href":"https:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/wp-json\/wp\/v2\/pages\/336"}],"collection":[{"href":"https:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":48,"href":"https:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/wp-json\/wp\/v2\/pages\/336\/revisions"}],"predecessor-version":[{"id":400,"href":"https:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/wp-json\/wp\/v2\/pages\/336\/revisions\/400"}],"wp:attachment":[{"href":"https:\/\/dev.pixelprofessionals.com\/webdevelopmentreference\/wp-json\/wp\/v2\/media?parent=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}