gpt4 book ai didi

PHP 控制台脚本/传递默认参数/重构 fopen() fread() fwrite() fclose()

转载 作者:可可西里 更新时间:2023-11-01 01:00:45 24 4
gpt4 key购买 nike

我写了这个小脚本来替换 Ubuntu Gnome 的 Numix 主题的颜色:

<?php
$oldColor = $argv[1];
$newColor = $argv[2];
// defaults
// $oldColor = 'd64937';
// $newColor = 'f66153';

$path = '/usr/share/themes/Numix/gtk-3.0/gtk-dark.css';
$fileRead = fopen($path, 'r');
$contents = fread($fileRead, filesize($path));
$newContents = str_replace($oldColor, $newColor, $contents);
$fileWrite = fopen($path, 'w');
fwrite($fileWrite, $newContents);
fclose($fileWrite);
?>

只要我传递两个参数,脚本就会按预期工作。

  1. 如何为参数设置默认值?
  2. 我是否应该使用 file_put_contents() 进行重构?

最佳答案

<?php 
// How do I set defaults for the arguments?
$oldColor = !empty($argv[1]) ? $argv[1] : 'd64937';
$newColor = !empty($argv[2]) ? $argv[2] : 'f66153';
$file = '/usr/share/themes/Numix/gtk-3.0/gtk-dark.css';

// Your choice whether its cleaner, I think so.
file_put_contents(
$file,
str_replace(
$oldColor,
$newColor,
file_get_contents($file)
)
);
?>

关于PHP 控制台脚本/传递默认参数/重构 fopen() fread() fwrite() fclose(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27753294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com