PHP test
To test php environment whether it is installed correctly or php settings.
<?php phpinfo(); ?>
It will show php all configurations.
PHP variable
Variable syntax
- Start with $ sign
- Name the variable
- no space
- no capital letter
- variable names are case sensitive
- no symbol
- variable types
- string: ‘Dhaka’;
- integer: 123456;
- no quotation
- floating number: 1.23456;
- no quotation
- boolean true false
- 0 = fales
- 1 = true
- array();
- array(‘one’, ‘two’);
-
array( array( 'one', 'two' ) );
Example of print variables
Integer
<?php $roll = 6; ?> <h1>My roll number is <?php echo $roll; ?>.</h1>
String (way one)
<?php $city = 'Dhaka and Rajshahi'; ?> <h1>Two most beautiful cities are <?php echo $city; ?>.</h1>
String (way two)
- using php concat
- ‘ . . ‘
<?php $city = 'Dhaka and Rajshahi'; echo '<h1>Two most beautiful cities are '.$city.'.</h1>' ?>
PHP if
-
<?php if() : ?> <?php endif; ?>,
<?php $beautiful = 'Rajshahi'; ?> <?php if($beautiful == 'Rajshahi') : ?> <h1>The most beautiful city is $beautiful.</h1> <?php endif; ?>
PHP if else – false value
<?php $beautiful = 'Dhaka'; ?> <?php if($beautiful == 'Rajshahi') : ?> <h1>The most beautiful city is $beautiful.</h1> <?php else : ?> <h1>We don't know</h1> <?php endif; ?>
PHP if else – true value
<?php $beautiful = 'Rajshahi'; ?> <?php if($beautiful == 'Rajshahi') : ?> <h1>The most beautiful city is $beautiful.</h1> <?php else : ?> <h1>We don't know</h1> <?php endif; ?>
PHP if else – all in one php tag
-
<?php if() { echo ''; } else { echo ''; }
<?php $beautiful = 'Rajshahi'; if($beautiful == 'Rajshahi') { echo '<h1>The most beautiful city is '.$beautiful.'.</h1>'; } else { echo '<h1>We don\'t know</h1>'; } ?>
PHP array()
- array data point starts with 0 then 1, 2, 3 …..
<?php $cities = array('Dhaka', 'Rajshahi', 'Khulna', 'Barisal'); echo '<h1>The second city is '.$cities[1].'.</h1>'; ?>
PHP array() with if else
<?php $cities = array('Dhaka', 'Rajshahi', 'Khulna', 'Barisal'); if($cities[3] == 'Rajshahi') { echo '<h1>The forth city is '.$cities[3].'.</h1>'; } else { echo '<h1>We don\'t know.</h1>'; } ?>
PHP equal variable
<?php $city1 = 'Dhaka'; $city2 = 'Dhaka'; if($city1 == $city2) { echo '<h1>The name of my city is '.$city1.'.</h1>'; } else { echo '<h1>We don\'t know.</h1>'; } ?>