Difference Between echo & Print in PHP

I searched for ‘difference between echo and print in php’, but got different answer with different logics. Here is what I understood after reading different blogs, and tutes. Basically both keywords echo and print work same. they give output to the user, but there are some minor differences as follows:

echo print
gives no return value. gives return value i.e. 1(one)
It can’t be used in expression, because it doesn’t return any value. can be used in expression, because it returns value.

For example, if you write echo print 1;

you’ll get the result: 11

It can take multiple parameters.

For example:

echo "This ", "string ", "was ", "made ", "with multiple parameters.";

It can take only one parameter.
It’s faster It’s slower

Pascal Triangle using PHP

We often hear about the pascal triangle while we go for the interview or when we start learning any programming language. Here is a good example of pascal triangle using php language.

Pattern:

pascal triangle


  1. <?php
  2.  
  3. $lines = 5;
  4. $star_times = 1;
  5. $blank_times = $lines - $star_times;
  6. $pyramid_char= '<span style="color:#000;">*</span>';
  7. $outer_char ='<span style="color:#fff;">*</span>';
  8.  
  9. for ($i = 1; $i <= $lines; $i++) {
  10. //Write blank line or whatever you like outside(before) the pyramid shape
  11. for ($blank_before = 0; $blank_before < $blank_times; $blank_before++) {
  12. echo $outer_char;
  13. }
  14. //Write the character to shape a pyramid
  15. for ($star = 1; $star <= $star_times; $star++) {
  16. echo $pyramid_char;
  17. }
  18. //Write blank line or whatever you like outside(after) the pyramid shape
  19. for ($blank_after = 0; $blank_after < $blank_times; $blank_after++) {
  20. echo $outer_char;
  21. }
  22. //increase the star and decrease the space
  23. $star_times = $star_times + 2;
  24. $blank_times = $blank_times -1;
  25. echo '<br/>';
  26. }
  27.  
  28. ?>

Type of Asset Location in Yii2

Assets, based on their location, can be classified as:

Source Assets

Source asset files are available together with PHP source code which is not accessible directly via the web. To use source assets in a page, they should be copied to a web directory and turned into the so-called published assets. This process is called asset publishing which will be described in detail shortly.

Published Assets

Published asset files are available in a web accessible directory and can thus be directly accessed via the web.

External Assets

The asset files are located on a Web server that is different from the one hosting your Web application.

Reference: Yii2 Doc

Remove index.php from URL in Yii2

To remove index.php from your URL in Yii 2.* add the following code to {application folder}/web/.htaccess

  1. RewriteEngine on
  2. # If a directory or a file exists, use it directly
  3. RewriteCond %{REQUEST_FILENAME} !-f
  4. RewriteCond %{REQUEST_FILENAME} !-d
  5. # Otherwise forward it to index.php
  6. RewriteRule . index.php

 

And add the following line to configuration file inside the component section

  1. 'urlManager' => [
  2. 'class' => 'yii\web\UrlManager',
  3. 'showScriptName' => false,
  4. 'enablePrettyUrl' => true,
  5. 'rules' => array(
  6. '<controller:\w+>/<id:\d+>' => '<controller>/view',
  7. '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
  8. '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
  9. ),
  10. ],

 

How to customize custom options price in frontend of magento

First of all let you go to download an extension Magebuzz_Customoption to automatically select one of the specified options in the drop-down. After installing you’ll see a menu in the admin panel to automatically select one option by default. 

screenshot-magento-default-custom-option

 

Create custom option as much as you need. Let’s assume 4 custom option (Material, Thickness, Width, Ring Size)

Add multiple options to these custom option with its value.

Now here is the calculation I need

 

  1. Final Price= ?
  2.  
  3. Material Price = user defined value
  4.  
  5. Final Price=Material Price
  6.  
  7. Thickness Price = Final Price * Thickness Price Value
  8.  
  9. Final Price=Thickness Price
  10.  
  11. Width Price = (Final Price / 8 ) * Width Price Value
  12.  
  13. Final Price=Width Price
  14.  
  15. Ring Size Price = Final Price + (% of Ring Size Price Value)
  16.  
  17. Final Price=Ring Size Price

To show that calculation on pages of cart, etc, let’s go to \app\code\core\Mage\Catalog\Model\Product\Type\Price.php

Edit _applyOptionsPrice method of Mage_Catalog_Model_Product_Type_Price class 

 

  1. protected function _applyOptionsPrice($product, $qty, $finalPrice) {
  2.  
  3. if ($optionIds = $product->getCustomOption('option_ids')) {
  4. $basePrice = $finalPrice;
  5. foreach (explode(',', $optionIds->getValue()) as $optionId) {
  6. if ($option = $product->getOptionById($optionId)) {
  7. $confItemOption = $product->getCustomOption('option_' . $option->getId());
  8.  
  9. $group = $option->groupFactory($option->getType())
  10. ->setOption($option)
  11. ->setConfigurationItemOption($confItemOption);
  12.  
  13. if (in_array($option['default_title'], array('material', 'Material'))) {
  14. // MP = MP;
  15. $finalPrice = $finalPrice + $group->getOptionPrice($confItemOption->getValue(), $basePrice);
  16. } elseif (in_array($option['default_title'], array('Thickness', 'thickness'))) {
  17. // TP = MP * TV;
  18. $thikness_price = $finalPrice * $group->getOptionPrice($confItemOption->getValue(), $basePrice);
  19. $finalPrice = $thikness_price;
  20. } elseif (in_array($option['default_title'], array('Width', 'width'))) {
  21. //WP = TP / 8 * WV;
  22. $width_price = ($finalPrice / 8 ) * $group->getOptionPrice($confItemOption->getValue(), $basePrice);
  23. $finalPrice = $width_price;
  24. } elseif (in_array($option['default_title'], array('Ring Size', 'ring size', 'Ring size', 'ring Size', 'ring_size'))) {
  25. //RSP = WP * RS%;
  26. $rsp = $finalPrice + (($finalPrice / 100) * $group->getOptionPrice($confItemOption->getValue()) );
  27. $finalPrice = $rsp;
  28. } else {
  29. $finalPrice += $group->getOptionPrice($confItemOption->getValue(), $basePrice);
  30. }
  31. }
  32. }
  33. }
  34.  
  35. return $finalPrice;
  36. }

If now you’ll see your product detail page & your cart page, both price will be differ, because, till now you have only configured custom price on cart & onward pages. Now let’s move to customize product detail page custom price

First of all we’ll go to \app\code\local\Magebuzz\Customoption\Block\Catalog\Product\View\Options\Type\Select.php to add a new html attribute to select element. 

  1. Add
  2.  
  3.  
  4. $extraParams .= ' label="' . $_option->getTitle() . '"';
  5.  
  6. after
  7.  
  8. $extraParams .= ' onchange="opConfig.reloadPrice()"';

Now let’s go to assign label of custom option to a variable which’ll be sent ahead for further process.

search for : if (typeof (configOptions[selectOption.value]) != ‘undefined’)

inside the if block add this code

  1. configOptions[selectOption.value].options_label = element.getAttribute("label");

Now let’s go to js file which will change price dynamically according to your choice on the product detail page. 

File path: \js\varien\product.js

search for : Object.values(this.customPrices).each(function (pair)

inside that add this :

  1. if (pair.excludeTax && pair.includeTax) {
  2. if (pair.options_label == 'Material') {
  3. //MP : Material Price
  4. subPrice += parseFloat(pair.excludeTax);
  5. subPriceincludeTax += parseFloat(pair.includeTax);
  6. } else if (pair.options_label == 'Thickness') {
  7. //TP : Thinkness price
  8. var thikness_price = subPrice * pair.price;
  9. subPrice = parseFloat(thikness_price);
  10. subPriceincludeTax = parseFloat(thikness_price);
  11. } else if (pair.options_label == 'Width') {
  12. //WP : Width Price
  13. var width_price = (subPrice / 8) * pair.price;
  14. subPrice = parseFloat(width_price);
  15. subPriceincludeTax = parseFloat(width_price);
  16.  
  17. } else if (pair.options_label == 'Ring Size') {
  18. //RSP : Ring Size Price
  19. var rsp = subPrice + ((subPrice / 100) * pair.price);
  20. //var width_price = (subPrice / 8 ) * pair.price;
  21. subPrice = parseFloat(rsp);
  22. subPriceincludeTax = parseFloat(rsp);
  23. } else {
  24. subPrice += parseFloat(pair.excludeTax);
  25. subPriceincludeTax += parseFloat(pair.includeTax);
  26. }
  27. //subPrice += parseFloat(pair.excludeTax);
  28. //subPriceincludeTax += parseFloat(pair.includeTax);
  29. } else {
  30. if (pair.options_label == 'Material') {
  31. //MP : Material Price
  32. subPrice += parseFloat(pair.excludeTax);
  33. subPriceincludeTax += parseFloat(pair.includeTax);
  34. } else if (pair.options_label == 'Thickness') {
  35. //TP : Thinkness price
  36. var thikness_price = subPrice * pair.price;
  37. subPrice = parseFloat(thikness_price);
  38. subPriceincludeTax = parseFloat(thikness_price);
  39. } else if (pair.options_label == 'Width') {
  40. //WP : Width Price
  41. var width_price = (subPrice / 8) * pair.price;
  42. subPrice = parseFloat(width_price);
  43. subPriceincludeTax = parseFloat(width_price);
  44.  
  45. } else if (pair.options_label == 'Ring Size') {
  46. //RSP : Ring Size Price
  47. var rsp = subPrice + ((subPrice / 100) * pair.price);
  48. //var width_price = (subPrice / 8 ) * pair.price;
  49. subPrice = parseFloat(rsp);
  50. subPriceincludeTax = parseFloat(rsp);
  51. } else {
  52. subPrice += parseFloat(pair.price);
  53. subPriceincludeTax += parseFloat(pair.price);
  54. }
  55. // subPrice += parseFloat(pair.price);
  56. // subPriceincludeTax += parseFloat(pair.price);
  57. }

Now you’ll be able to see the custom price on product detail page as well