<?php
$find = [
// Spezial
'<',
'>',
'"',
'&gt;',
'required',
'&lt;?php','?<span style="color: red">&gt;</span>',
// Element
'&lt;!doctype','&lt;/!doctype',
'&lt;a','&lt;/a',
'&lt;aside','&lt;/aside',
'&lt;br',
'&lt;body','&lt;/body',
'&lt;button','&lt;/button',
'&lt;caption','&lt;/caption',
'&lt;div','&lt;/div',
'&lt;fieldset','&lt;/fieldset',
'&lt;footer','&lt;/footer',
'&lt;form','&lt;/form',
'&lt;h1','&lt;/h1',
'&lt;h2','&lt;/h2',
'&lt;h3','&lt;/h3',
'&lt;head','&lt;/head',
'&lt;header','&lt;/header',
'&lt;html','&lt;/html',
'&lt;img',
'&lt;input','&lt;/input',
'&lt;label','&lt;/label',
'&lt;legend','&lt;/legend',
'&lt;li','&lt;/li',
'&lt;link','&lt;/link',
'&lt;meta',
'&lt;mark','&lt;/mark',
'&lt;ol','&lt;/ol',
'&lt;p','&lt;/p',
'&lt;strong','&lt;/strong',
'&lt;style','&lt;/style',
'&lt;table','&lt;/table',
'&lt;td','&lt;/td',
'&lt;textarea','&lt;/textarea',
'&lt;th','&lt;/th',
'&lt;title','&lt;/title',
'&lt;tr','&lt;/tr',
'&lt;ul','&lt;/ul',
// Attribut
'action=',
'class=',
'href=',
'href^=',
'id=',
'lang=',
'method=',
'max=',
'min=',
'name=',
'number=',
'placeholder=',
'rel=',
'title=',
'type=',
'value=',
'xmlns=',
// Attribut Value
'number',
'post',
'Reset',
'reset',
'Submit',
'submit',
// CSS
'border:',
'background:',
'height:',
'margin:',
'max-width:',
'padding:',
'position:',
'width:'
];
$repl = [
// Spezial
'&lt;',
'&gt;',
'<span style="color: blue">&quot;</span>',
'<span style="color: red">&gt;</span>',
'<strong>required</strong>',
'<span style="color: orange">&lt;?php</span>','<span style="color: orange">?&gt;</span>',
// Element
'<span style="color: red">&lt;!doctype</span>','<span style="color: red">&lt;/!doctype</span>',
'<span style="color: red">&lt;a</span>','<span style="color: red">&lt;/a</span>',
'<span style="color: red">&lt;aside</span>','<span style="color: red">&lt;/aside</span>',
'<span style="color: red">&lt;br</span>',
'<span style="color: red">&lt;body</span>','<span style="color: red">&lt;/body</span>',
'<span style="color: red">&lt;button</span>','<span style="color: red">&lt;/button</span>',
'<span style="color: red">&lt;caption</span>','<span style="color: red">&lt;/caption</span>',
'<span style="color: red">&lt;div</span>','<span style="color: red">&lt;/div</span>',
'<span style="color: red">&lt;fieldset</span>','<span style="color: red">&lt;/fieldset</span>',
'<span style="color: red">&lt;footer</span>','<span style="color: red">&lt;/footer</span>',
'<span style="color: red">&lt;form</span>','<span style="color: red">&lt;/form</span>',
'<span style="color: red">&lt;h1</span>','<span style="color: red">&lt;/h1</span>',
'<span style="color: red">&lt;h2</span>','<span style="color: red">&lt;/h2</span>',
'<span style="color: red">&lt;h3</span>','<span style="color: red">&lt;/h3</span>',
'<span style="color: red">&lt;head</span>','<span style="color: red">&lt;/head</span>',
'<span style="color: red">&lt;header</span>','<span style="color: red">&lt;/header</span>',
'<span style="color: red">&lt;html</span>','<span style="color: red">&lt;/html</span>',
'<span style="color: red">&lt;img</span>',
'<span style="color: red">&lt;input</span>','<span style="color: red">&lt;/input</span>',
'<span style="color: red">&lt;label</span>','<span style="color: red">&lt;/label</span>',
'<span style="color: red">&lt;legend</span>','<span style="color: red">&lt;/legend</span>',
'<span style="color: red">&lt;li</span>','<span style="color: red">&lt;/li</span>',
'<span style="color: red">&lt;link</span>','<span style="color: red">&lt;/link</span>',
'<span style="color: red">&lt;meta</span>',
'<span style="color: red">&lt;mark</span>','<span style="color: red">&lt;/mark</span>',
'<span style="color: red">&lt;ol</span>','<span style="color: red">&lt;/ol</span>',
'<span style="color: red">&lt;p</span>','<span style="color: red">&lt;/p</span>',
'<span style="color: red">&lt;strong</span>','<span style="color: red">&lt;/strong</span>',
'<span style="color: red">&lt;style</span>','<span style="color: red">&lt;/style</span>',
'<span style="color: red">&lt;table</span>','<span style="color: red">&lt;/table</span>',
'<span style="color: red">&lt;td</span>','<span style="color: red">&lt;/td</span>',
'<span style="color: red">&lt;textarea</span>','<span style="color: red">&lt;/textarea</span>',
'<span style="color: red">&lt;th</span>','<span style="color: red">&lt;/th</span>',
'<span style="color: red">&lt;title</span>','<span style="color: red">&lt;/title</span>',
'<span style="color: red">&lt;tr</span>','<span style="color: red">&lt;/tr</span>',
'<span style="color: red">&lt;ul</span>','<span style="color: red">&lt;/ul</span>',
// Attribut
'<span style="color: blue">action=</span>',
'<span style="color: blue">class=</span>',
'<span style="color: blue">href=</span>',
'<span style="color: blue">href^=</span>',
'<span style="color: blue">id=</span>',
'<span style="color: blue">lang=</span>',
'<span style="color: blue">method=</span>',
'<span style="color: blue">max=</span>',
'<span style="color: blue">min=</span>',
'<span style="color: blue">name=</span>',
'<span style="color: blue">number=</span>',
'<span style="color: blue">placeholder=</span>',
'<span style="color: blue">rel=</span>',
'<span style="color: blue">title=</span>',
'<span style="color: blue">type=</span>',
'<span style="color: blue">value=</span>',
'<span style="color: blue">xmlns=</span>',
// Attribut Value
'<span style="color: green">number</span>',
'<span style="color: green">post</span>',
'<span style="color: green">Reset</span>',
'<span style="color: green">reset</span>',
'<span style="color: green">Submit</span>',
'<span style="color: green">submit</span>',
// CSS
'<span style="color: blue">border:</span>',
'<span style="color: blue">background:</span>',
'<span style="color: blue">height:</span>',
'<span style="color: blue">margin:</span>',
'<span style="color: blue">max-width:</span>',
'<span style="color: blue">padding:</span>',
'<span style="color: blue">position:</span>',
'<span style="color: blue">width:</span>'
];
$codcol = '
<button syle="color: red">Button</button>
<input name="input" value="tru">
<br>
';
echo str_replace ($find, $repl, $codcol);


 
by

PHP Online Compiler

Write, Run & Share PHP code online using OneCompiler's PHP online compiler for free. It's one of the robust, feature-rich online compilers for PHP language, running on the latest version 7. Getting started with the OneCompiler's PHP compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as PHP and start coding.

Taking inputs (stdin)

OneCompiler's PHP online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample PHP program which takes name as input and prints hello message with your name.

<?php
	fscanf(STDIN, "%s\n", $name);           
    echo "Hello ".$name.".\n";
?>

About PHP

PHP(Hypertext Preprocessor) is widely used server sripting language by Rasmus Lerdorf in the year 1994.

Key features

  • Free
  • powerful tool for making dynamic and interactive web pages
  • can integrate with almost all popular databases like MySQL, PostgreSQL, Oracle, Sybase, Informix, Microsoft SQL Server etc.
  • C like Syntax and easy to learn.
  • Object oriented scripting language.
  • easily embeddable into HTML
  • Loosely typed language.

Syntax help

Variables

In PHP, there is no need to explicitly declare variables to reserve memory space. When you assign a value to a variable, declaration happens automatically. Variables are case-sensitive in PHP.

$variable_name = value;  

Loops

1. IF Family:

If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.

If

if(conditional-expression){    
//code    
} 

If-else

if(conditional-expression){  
//code if condition is true  
} else {  
//code if condition is false  
} 

Nested-If-else

if(condition-expression1) {  
    //code if above condition is true  
} elseif(condition-expression2){  
    //code if above condition is true  
}  
elseif(condition-expression3) {  
    //code if above condition is true  
}  
...  
else {  
    //code if all the conditions are false  
}  

2. Switch:

Switch is used to execute one set of statement from multiple conditions.

switch(conditional-expression) {    
case value1:    
 // code if the above value is matched    
 break;  // optional  
case value2:    
 // code if the above value is matched    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 
 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

For-each:

// you can use any of the below syntax
foreach ($array as $element-value) {  
    //code  
}

foreach ($array as $key => $element-value) {   
    //code 
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

How to define a Function

function function_name(parameters) {  
  //code
}

How to call a Function

function_name (parameters)