{"id":44169,"date":"2022-09-19T09:15:45","date_gmt":"2022-09-19T08:15:45","guid":{"rendered":"https:\/\/13node.com\/sin-categorizar\/passing-variables-to-an-include-in-php\/"},"modified":"2025-08-05T20:32:11","modified_gmt":"2025-08-05T19:32:11","slug":"passing-variables-to-an-include-in-php","status":"publish","type":"post","link":"https:\/\/13node.com\/en\/it-en\/passing-variables-to-an-include-in-php\/","title":{"rendered":"Passing Variables to an Include in PHP"},"content":{"rendered":"<p>If we have ever had the need to pass variables in an include to make it dynamic, this is very common when we reuse components.<\/p>\n<p>I&#8217;ll leave you with a feature that will save you the work.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">\/\/ Include con Variables\nfunction includeWithVariables($filePath, $variables = array(), $print = true)\n{\n    $output = NULL;\n    if(file_exists($filePath)){\n        \/\/ Extrae las variables en un entorno local\n        extract($variables);\n\n        \/\/ Empieza el buffering de salida\n        ob_start();\n\n        \/\/ Include el archivo\n        include $filePath;\n\n        \/\/ Termina el buffering y devuelve su contenido\n        $output = ob_get_clean();\n    }\n    if ($print) {\n        print $output;\n    }\n    return $output;\n\n}<\/pre>\n<p>After placing our function, we can use it wherever we want by passing it the variables.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php includeWithVariables('detalles.php', array('detalle' =&gt; 'Hola')); ?&gt;<\/pre>\n<p>And inside our PHP file called details.php we could call any variable simply with<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;? echo $detalle; ?&gt;\n<\/pre>\n<p>As you&#8217;ve seen, the solution for passing variables or parameters to an include is very simple. We hope to continue helping, and if you have any questions, don&#8217;t hesitate to leave a comment. <\/p>\n<h2>Safety tip<\/h2>\n<p>If the variables you pass to the include come from forms or URLs (<code>$_POST<\/code>,  <code>$_GET<\/code>, etc.), it is good practice to sanitize them before passing them, to avoid security problems such as XSS or injections.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If we have ever had the need to pass variables in an include to make it dynamic, this is very<\/p>\n","protected":false},"author":1,"featured_media":43676,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_joinchat":[],"footnotes":""},"categories":[1110,1085],"tags":[],"class_list":["post-44169","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-snippets-en","category-it-en","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/13node.com\/en\/wp-json\/wp\/v2\/posts\/44169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/13node.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/13node.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/13node.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/13node.com\/en\/wp-json\/wp\/v2\/comments?post=44169"}],"version-history":[{"count":1,"href":"https:\/\/13node.com\/en\/wp-json\/wp\/v2\/posts\/44169\/revisions"}],"predecessor-version":[{"id":44546,"href":"https:\/\/13node.com\/en\/wp-json\/wp\/v2\/posts\/44169\/revisions\/44546"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/13node.com\/en\/wp-json\/wp\/v2\/media\/43676"}],"wp:attachment":[{"href":"https:\/\/13node.com\/en\/wp-json\/wp\/v2\/media?parent=44169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/13node.com\/en\/wp-json\/wp\/v2\/categories?post=44169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/13node.com\/en\/wp-json\/wp\/v2\/tags?post=44169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}